标签:this 赋值 prototype creat 属性 方法 class col alert
1、call 继承
function Person(name,age){ this.name = name; this.age = age; } function Coder(name,age,job){ Person.call(this,name,age);//改变了this指向 this.job = job; // console.log(this);//Coder } let p = new Person(‘a‘,18); let c = new Coder(‘ab‘,16,‘猿妹‘); console.log(c);//这样就继承了Person下的一些属性,
2、拷贝继承
function Person(name,age){ this.name = name; this.age = age; } Person.prototype.say = function(){ console.log(‘我叫‘+this.name); } Person.prototype.runing = function(){ console.log(‘我会跑‘); } function Coder(name,age,job){ Person.call(this,name,age); this.job = job; } // Coder.prototype = Person.prototype; //此时赋址了 Object.prototype.aa = 20; for(let attr in Person.prototype){ //因为in会遍历原型,所以只有父类原型上的属性才会赋值 if(Person.prototype.hasOwnProperty(attr)){ Coder.prototype[attr] = Person.prototype[attr]; } } Coder.prototype.runing = function(){ console.log(‘会骑UFO‘); } let p = new Person(‘a‘,18); let c = new Coder(‘b‘,16,‘前端‘); c.runing(); p.runing(); console.log(Coder.prototype);//
3、
function Person(name,age){ this.name = name; this.age = age; } Person.prototype.say = function(){ alert(‘我的名字‘+this.name); } Person.prototype.runing = function(){ alert(‘我会跑‘); } function Coder(name,age,job){ Person.call(this,name,age); this.job = job; } Coder.prototype = Object.create(Person.prototype); // Coder.prototype = Object.assign({},Person.prototype); //类似拷贝 // Object.assign(Coder.prototype,Person.prototype) Coder.prototype.say = function(){ alert(‘duang,duang,duang,123!~‘); } let c = new Coder(‘a‘,26,‘超级码农‘); let p = new Person(‘b‘,26); c.say(); p.say();
标签:this 赋值 prototype creat 属性 方法 class col alert
原文地址:https://www.cnblogs.com/Allisson/p/9902065.html