原型

(一)需要很少的实例化对象
function Person() { } Person.prototype.name = 'xiuru' Person.prototype.age = 27 Person.prototype.do = function() { alert(this.name) } var person1 = new Person() // 实例化 person1.do() person1.__proto__.name = 'ruyi' console.log(person1.name) console.log(Person.prototype) // 判断原型是否属于某个实例 console.log(Person.prototype.isPrototypeOf(person1))

(二)实例化对象很多(不管多少推荐使用下面方法)
// 如果你的实例化特别多的时候,就需要用到了原型 function Dog(name,age,hobby) { this.name = name this.age = age this.hobby = hobby } Dog.prototype.action = function () { return this.name } var dog1 = new Dog ('maomao',14,'wangwangjiao') var dog2 = new Dog('xiaomao',2,'eat')

(三)继承

function Parent() {} Parent.prototype.action = function () { console.log('吃饭,睡觉,打呼噜。。。。') } function Child(){ this.action = function (){ console.log('不吃饭,不睡觉,不打呼噜。。。。') } } Child.prototype = new Parent(); var person1 = new Child() person1.action = function () { console.log('以上两种都不是') } person1.action()

 总结:为了以后方便查阅

原文地址:https://www.cnblogs.com/linjiu0505/p/10737742.html