一、实例
Person.prototype.name = 'sunny';
function Person() {}
var person = new Person();
Person.prototype.name = 'cherry';
console.log(person.name); //cherry
Person.prototype.name = 'sunny';
function Person() {}
Person.prototype.name = 'cherry';
var person = new Person();
console.log(person.name); //cherry
Person.prototype.name = 'sunny';
function Person() {
//var this = {__proto__ : Person.prototype}
}
var person = new Person();
Person.prototype = {
name : 'cherry'
}
console.log(person.name); //sunny
这样理解
var obj = {name : "a"};
var obj1 = obj;
obj = {name : "b"};
Person.prototype = {name : "a"};
__proto__ = Person.prototype;
Person.prototype = {name : "b"};
new Person最后执行
Person.prototype.name = 'sunny';
function Person() {
// var this = {__proto__ : Person.prototype}
}
Person.prototype = {
name : 'cherry'
}
var person = new Person();
console.log(person.name); //cherry
二、隐式添加
Person.prototype.name = 'abc';
function Person() {
// var this = {
// __proto__ : Person.prototype
// }
}
var person = new Person();
三、继承
// 继承
//1.圣杯模式
function inherit(Target, Origin) {
function F() {}
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype;
}
// 2.雅虎写法 闭包
var inherit = (function () {
var F = function () {}
return function (Target, Origin) {
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype;
}
}());
Father.prototype.lastName = "G";
function Father() {}
function Son() {}
inherit(Son, Father);
var son = new Son();
var father = new Father();