//声明超类,通过构造函数和原型添加有关属性和方法
function Super(){
this.property = true;
}
Super.prototype.getSuperValue = function() {
return this.property;
};
//声明子类的构造函数
function SubType() {
this.subproperty = false;
}
//将子类的原型指向超类的实例,得到超类的一切
SubType.prototype = new Super();
SubType.prototype.constructor = SubType;
SubType.prototype.getSubValue = function(){
return this.subproperty;
};
//由子类创建对象,测试是否继承超类方法和属性
var instance = new SubType();
console.log(instance.getSuperValue());
instance instanceof Object; Object.prototype.isPrototypeOf(instance);
......
SubType.prototype = new Super();
SubType.prototype = {
....
};
function hasPrototypeProperty(object, name) {
return name in object && !object.hasOwnProperty(name);
}
function Person(name) {
this.name = name;
}
Person.prototype = {
constructor: Person,
sayName: function () {
console.log(this.name);
},
toString: function() {
}
};
var person1 = new Person('Nicholas');
var person2 = new Person('Greg);
console.log(person1 instanceof Person); // true
console.log(person1.constructor === Person); // true
console.log(person1.constructor === Object); // false
console.log(person2 instanceof Person); // true
console.log(person2.constructor === Person); // true
console.log(person2.constructor === Object); // false
var person1 = {
name: 'Nicholas',
sayName: function () {
console.log(this.name);
}
};
var person2 = Object.create(person1, {
name: {
configurable: true,
enumerable: true,
value: 'Greg',
writable: true
}
});
person1.sayName(); // Nicholas
person2.sayName(); // Greg
console.log(person1.hasOwnProperty('sayName')); // true
console.log(person1.isPropertyOf(person2)); // true
console.log(person2.hasOwnProperty('sayName')); // false
var person = (function () {
var age = 25;
function getAge() {
return age;
}
function growOlder() {
age++;
}
return {
name: 'Nicholas',
getAge: getAge,
growOlder: growOlder
};
}());
function Person(name) {
this.name = name;
}
Person.prototype.sayName = function() {
console.log(this.name);
};
var person1 = Person('Nicholas');
console.log(person1 instanceof Person); // false
console.log(typeof person1); // undefined
console.log(name); // Nicholas
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有