[ Web Proxy ]
URL:
Viewing: https://ar.javascript.info/constructor-new [Back]  [Original]

"new"
:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

"new"

{...}. . (constructor functions) "new".

:

  1. .
  2. "new" . :
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name); // Jack
alert(user.isAdmin); // false

new :

  1. this.
  2. . this .
  3. this. new User(...) :
function User(name) {
// this = {}; (implicitly)
// this   
this.name = name;
this.isAdmin = false;
// this; (implicitly) 
}

let user = new User("Jack") :

let user = {
name: "Jack",

isAdmin: false
};

new User("Ann") new User("Alice") . . . . new . new.

**new function() { }**

:

let user = new function() {
this.name = "John";
this.isAdmin = false;
// ...   
//      
//   ..
};

. .

: new.target

: . new new.target. new:

Inside a function, we can check whether it was called with `new` or without it, using a special `new.target` property.

It is undefined for regular calls and equals the function if called with `new`:

```js run
function User() {
alert(new.target);
}
// "new" :
User(); // undefined
//  "new":
new User(); // function User { ... }

new . new - - :

function User(name) {
if (!new.target) { // new    
return new User(name); // new ...
}
this.name = name;
}
let john = User("John"); // new User   
alert(john.name); // John

new . new . new .

return. this . return :

  • return this.
  • return . return this. return this:
function BigUser() {
this.name = "John";
return { name: "Godzilla" }; // <--   
}
alert( new BigUser().name ); // Godzilla,   

return ( ):

function SmallUser() {
this.name = "John";
return; //  this 
}
alert( new SmallUser().name ); // John

return. .

new :

let user = new User; // <--   
//  
let user = new User();

.

. this . new User(name) name sayHi:

function User(name) {
this.name = name;
this.sayHi = function() {
alert( "My name is: " + this.name );
};
}
let john = new User("John");
john.sayHi(); // My name is: John
/*
john = {
name: "John",
sayHi: function() { ... }
}
*/

.

  • .
  • new . this .
  • .
  • JavaScript () : Date Set .

. . .

: 2 A B new A()==new B()

function A() { ... }
function B() { ... }
let a = new A;
let b = new B;
alert( a == b ); // true

. . new this. obj:

let obj = {};
function A() { return obj; }
function B() { return obj; }

alert( new A() == new B() ); // true

: 5 Calculator :

  • read() .
  • sum() .
  • mul() . :
let calculator = new Calculator();
calculator.read();
alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );

function Calculator() {
this.read = function() {
this.a = +prompt('a?', 0);
this.b = +prompt('b?', 0);
};
this.sum = function() {
return this.a + this.b;
};
this.mul = function() {
return this.a * this.b;
};
}
let calculator = new Calculator();
calculator.read();
alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );

: 5 Accumulator(startingValue) :

  • value. startingValue .
  • read() prompt value. value startingValue. :
let accumulator = new Accumulator(1); //   1
accumulator.read(); //     
accumulator.read(); //     
alert(accumulator.value); //   

function Accumulator(startingValue) {
this.value = startingValue;
this.read = function() {
this.value += +prompt('How much to add', 0);
};
}
let accumulator = new Accumulator(1);
accumulator.read();
accumulator.read();
alert(accumulator.value);

-- Constructor, operator new The JavaScript Language

: 2

Is it possible to create functions A and B such as new A()==new B()?

function A() { ... }
function B() { ... }

let a = new A;
let b = new B;

alert( a == b ); // true

If it is, then provide an example of their code.

Yes, its possible.

If a function returns an object then new returns it instead of this.

So they can, for instance, return the same externally defined object obj:

let obj = {};

function A() { return obj; }
function B() { return obj; }

alert( new A() == new B() ); // true
: 5

Create a constructor function Calculator that creates objects with 3 methods:

  • read() asks for two values using prompt and remembers them in object properties.
  • sum() returns the sum of these properties.
  • mul() returns the multiplication product of these properties.

For instance:

let calculator = new Calculator();
calculator.read();

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );

sandbox .

function Calculator() {

  this.read = function() {
    this.a = +prompt('a?', 0);
    this.b = +prompt('b?', 0);
  };

  this.sum = function() {
    return this.a + this.b;
  };

  this.mul = function() {
    return this.a * this.b;
  };
}

let calculator = new Calculator();
calculator.read();

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );

sandbox.

: 5

Create a constructor function Accumulator(startingValue).

Object that it creates should:

  • Store the current value in the property value. The starting value is set to the argument of the constructor startingValue.
  • The read() method should use prompt to read a new number and add it to value.

In other words, the value property is the sum of all user-entered values with the initial value startingValue.

Heres the demo of the code:

let accumulator = new Accumulator(1); // initial value 1

accumulator.read(); // adds the user-entered value
accumulator.read(); // adds the user-entered value

alert(accumulator.value); // shows the sum of these values

sandbox .

function Accumulator(startingValue) {
  this.value = startingValue;

  this.read = function() {
    this.value += +prompt('How much to add?', 0);
  };

}

let accumulator = new Accumulator(1);
accumulator.read();
accumulator.read();
alert(accumulator.value);

sandbox.


Web Proxy Viewer  |  New URL  |  Original Page