, :
__extends - , - , , .
1:
, - , , , , false:
var value1 = true && true && function() {};
var value2 = true && false && function() {};
var value3 = true && function() {} && true;
, , , javascript, __extends.
2:
d (, ) b (, ) , .
3:
prototype -, ( , new <function name>()).
new , [[PROTOTYPE]] aka __proto__ .
function Person() {
}
var p = new Person();
console.log(p.__proto__ === Person.prototype);
console.log(Person.prototype.__proto__ === Object.prototype);
. .
,
var o = {};
console.log(o.__proto__ === Object.prototype);
__proto__ Object.prototype ( ).
__prototype__ , Object.create.
, [[PROTOTYPE]]. , TH. , -, Object.prototype. , .
4
Javascript " ".
function Girl() {
}
Girl.prototype = Object.create(Person.prototype);
console.log(Girl.prototype.__proto__ === Person.prototype);
console.log(Girl.constructor === Function);
Girl.constructor = Girl;
console.log(Girl.constructor);
, Girl, Person Function.
: http://jsbin.com/dutojo/1/edit?js,console
:
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
:
var __extends = (this && this.__extends) || (function () {
})();
1 , ( ) __extends, .
(this && this.__extends)
, 1. .__ extends , __extends . , , || iife ( ).
gobbledygook, __extends:
var extendStatics = Object.setPrototypeOf ||
extendedStatics Object.setPrototypeOf , (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf)
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
3 __proto__ [[PROTOTYPE]] .
{ __proto__: [] } instanceof Array
- , , , __proto__, , Array.
1 , instanceof javascript true false (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof), , , extendsStatics
function (d, b) { d.__proto__ = b; })
, extensionStatics :
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }
, __proto__ ECMAScript, ECMAScript 2015 (, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto) ). , __proto__, " " , b d .
, exteStatics, , , exteStatics ( ). , 'd' (), 'b' ( ):
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
extendedStatics, (d) (b) (, 3 ):
extendStatics(d, b);
__, (d):
function __() { this.constructor = d; }
base (b) constructor null, , prototype.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor, Object.prototype.constructor ( javascript):
Object . , , , .
. , ( ) , .
, constructor '__' , .
:
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
prototype (d) , constructor
Object.create(b)
__ constructor prototype prototype, __(), constructor .
(__.prototype = b.prototype, new __()
, , .
B() ?
return _super !== null && _super.apply(this, arguments) || this;
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
apply() this, ( , ).
, B - , B.
Person ( ), , Girl ( ) .
function Person(n) {
this.name = n;
}
function Girl() {
Person.apply(this, arguments);
Girl.prototype = Object.create(Person.prototype);
Girl.prototype.constructor = Girl;
}
var p = new Person("Sally");
var g = new Girl("Trudy");
console.log(p.name);
console.log(g.name);