function Cat (name, color) {
this.name = name;
this.color = color;
}
var cat1 = new Cat('大毛', '白色');
cat1.name // '大毛'
cat1.color // '白色'
function Cat(name, color) {
this.name = name;
this.color = color;
this.meow = function () {
console.log('mew, mew, mew...');
};
}
var cat1 = new Cat('大毛', '白色');
var cat2 = new Cat('二毛', '黑色');
cat1.meow === cat2.meow
// false
function Animal (name) {
this.name = name;
}
Animal.prototype.color = "white";
var cat1 = new Animal('大毛');
var cat2 = new Animal('二毛');
cat1.color // 'white'
cat2.color // 'white'
Animal.prototype.color = "yellow"; cat1.color // 'yellow' cat2.color // 'yellow'
cat1.color = 'black'; cat2.color // 'yellow' Animal.prototype.color // "yellow";
Animal.prototype.walk = function () {
console.log(this.name + ' is walking.');
};
Object.getPrototypeOf(Object.prototype) // null
function MyArray (){}
MyArray.prototype = new Array();
MyArray.prototype.constructor = MyArray;
var mine = new MyArray();
mine.push(1, 2, 3);
mine.length // 3
mine instanceof Array // true
mine instanceof Array // 等同于 (Array === MyArray.prototype.constructor) || (Array === Array.prototype.constructor) || (Array === Object.prototype.constructor )
function P() {}
P.prototype.constructor === P
// true
function P() {}
var p = new P();
p.constructor
// function P() {}
p.constructor === P.prototype.constructor
// true
p.hasOwnProperty('constructor')
// false
function F(){};
var f = new F();
f.constructor === F // true
f.constructor === RegExp // false
// 空对象的原型是Object.prototype
Object.getPrototypeOf({}) === Object.prototype
// true
// 函数的原型是Function.prototype
function f() {}
Object.getPrototypeOf(f) === Function.prototype
// true
// 假定F为构造函数,f为F的实例对象
// 那么,f的原型是F.prototype
var f = new F();
Object.getPrototypeOf(f) === F.prototype
// true
var o1 = { p: 1 };
var o2 = Object.create(o1);
o2.p // 1
if (typeof Object.create !== "function") {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var o1 = Object.create({});
var o2 = Object.create(Object.prototype);
var o3 = new Object();
var o = Object.create(null); o.valueOf() // TypeError: Object [object Object] has no method 'valueOf'
Object.create() // TypeError: Object prototype may only be an Object or null
var o1 = { p: 1 };
var o2 = Object.create(o1);
o1.p = 2;
o2.p
// 2
var o = Object.create(Object.prototype, {
p1: { value: 123, enumerable: true },
p2: { value: "abc", enumerable: true }
});
o.p1 // 123
o.p2 // "abc"
var o1 = {};
var o2 = Object.create(o1);
var o3 = Object.create(o2);
o2.isPrototypeOf(o3) // true
o1.isPrototypeOf(o3) // true
var ClassDemo = function () {
//静态private变量
var private_static_var = 'aaaa';
//静态private方法
var private_static_func = function (key) {
return key + private_static_var;
}
//private方法,关键就是第一参数self要传入this
var private_func = function (self, key) {
return private_static_func(key + self.id);
}
var _class = function (id) { //构造函数
this.id = id; //public变量
}
//public方法
_class.prototype.public_func = function (key) {
return private_func(this, key);
}
return _class;
}();
var a = new ClassDemo('hello world');
alert(a.public_func('world hello'));
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有