{...}.
. (constructor functions)
"new".
:
- .
-
"new". :
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name); // Jack
alert(user.isAdmin); // false
new :
-
this. - .
this. -
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 :
-
returnthis. -
return.returnthis.returnthis:
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 () :
DateSet.
. . .
: 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()promptvalue.valuestartingValue. :
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);
<code><pre>10 (plnkr, JSBin, codepen)