var p = Object.getPrototypeOf(obj);
var obj = Object.create(p);
var obj = {};
var p = {};
obj.__proto__ = p;
Object.getPrototypeOf(obj) === p // true
var a = {x: 1};
var b = {__proto__: a};
b.x // 1
var o = new Foo(); // 等同于 var o = new Object(); o.__proto__ = Foo.prototype; Foo.call(o);
var a = { x: 1 };
var b = { __proto__: a };
var c = { __proto__: b };
c.x // 1
var o = {
a: 2,
m: function(b) {
return this.a + 1;
}
};
var p = Object.create(o);
p.a = 12;
p.m() // 13
function Shape() {
this.x = 0;
this.y = 0;
}
Shape.prototype.move = function (x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
Rectangle构造函数继承Shape。
function Rectangle() {
Shape.call(this); // 调用父类构造函数
}
// 另一种写法
function Rectangle() {
this.base = Shape;
this.base();
}
// 子类继承父类的方法
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
rect instanceof Rectangle // true
rect instanceof Shape // true
rect.move(1, 1) // 'Shape moved.'
ClassB.prototype.print = function() {
ClassA.prototype.print.call(this);
// some code
}
var obj = new Object(); obj.__proto__ === Object.prototype // true obj.__proto__ === obj.constructor.prototype // true
var P = function () {};
var p = new P();
var C = function () {};
C.prototype = p;
var c = new C();
c.constructor.prototype === p // false
C.prototype = p; C.prototype.constructor = C; c.constructor.prototype === p // true
var o = new Object(); Object.getPrototypeOf(o) === Object.prototype // true
Object.getPrototypeOf({ __proto__: null }) === null
vehicle.__proto__ = machine; car.__proto__ = vehicle;
Array.prototype.p = 'abc'; var a = new Array(); a.__proto__.p // abc a.constructor.prototype.p // abc
var f = function (){};
var a = {};
f.prototype = a;
var o = new f();
o.__proto__ === a
// true
Object.getOwnPropertyNames(Date) // ["parse", "arguments", "UTC", "caller", "name", "prototype", "now", "length"]
Object.keys(Date) // [] hasOwnProperty()
Date.hasOwnProperty('length')
// true
Date.hasOwnProperty('toString')
// false
var proto = { p1: 123 };
var o = Object.create(proto);
o.p1 // 123
o.hasOwnProperty("p1") // false
"length" in Date // true "toString" in Date // true
var o1 = {p1: 123};
var o2 = Object.create(o1,{
p2: { value: "abc", enumerable: true }
});
for (p in o2) {console.info(p);}
// p2
// p1
for ( var name in object ) {
if ( object.hasOwnProperty(name) ) {
/* loop code */
}
}
function inheritedPropertyNames(obj) {
var props = {};
while(obj) {
Object.getOwnPropertyNames(obj).forEach(function(p) {
props[p] = true;
});
obj = Object.getPrototypeOf(obj);
}
return Object.getOwnPropertyNames(props);
}
inheritedPropertyNames(Date) // ["caller", "constructor", "toString", "UTC", "call", "parse", "prototype", "__defineSetter__", "__lookupSetter__", "length", "arguments", "bind", "__lookupGetter__", "isPrototypeOf", "toLocaleString", "propertyIsEnumerable", "valueOf", "apply", "__defineGetter__", "name", "now", "hasOwnProperty"]
function copyObject(orig) {
var copy = Object.create(Object.getPrototypeOf(orig));
copyOwnPropertiesFrom(copy, orig);
return copy;
}
function copyOwnPropertiesFrom(target, source) {
Object
.getOwnPropertyNames(source)
.forEach(function(propKey) {
var desc = Object.getOwnPropertyDescriptor(source, propKey);
Object.defineProperty(target, propKey, desc);
});
return target;
}
function M1(prop) {
this.hello = prop;
}
function M2(prop) {
this.world = prop;
}
function S(p1, p2) {
this.base1 = M1;
this.base1(p1);
this.base2 = M2;
this.base2(p2);
}
S.prototype = new M1();
var s = new S(111, 222);
s.hello // 111
s.world // 222
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有