在js中,一切皆对象
原型对象里的所有属性和方法 被所有构造函数实例化出来的对象所共享 每次代码读取一个对象的属性的时候: 首先会进行一次搜索:搜索实例对象里的属性,,如果没有,再去对应的原型对象里去搜索属性 如果有就返回 没有返回undefined
isPRototypeOf(new instance) //判断原型的方法ECMA5: Object.getPrototypeOf() //根据实例对象获得原型对象hasOwnProperty('name') //判断是否是实例属性function Person(){ }Person.prototype.name = 'z3'; Person.prototype.age = 20 ; Person.prototype.sayName = function(){alert('我是原型对象的方法!')}; var p1 = new Person();alert(p1.name); // z3 var prototypeObj = Object.getPrototypeOf(p1);//根据实例对象获得原型对象alert(prototypeObj == Person.prototype);// in 操作符 判断属性是否存在于 实例对象和原型对象中// 在原型对象中 是否存在这个属性 第一个参数:当前对象 ,第二个参数:要判断的属性function hasprototypeProperty(object , name){ return !object.hasOwnProperty(name) && name in object ;}// ECMA5新特性 Object.keys(): 拿到当前对象里的所有keys 返回一个数组var p1 = new Person();p1.name = 'z3';p1.age = 20 ; var attributes = Object.keys(p1);alert(attributes);var attributes2 = Object.keys(Person.prototype);alert(attributes2);// ECMA5 constructor属性: 该属性是不能被枚举的[eable = false]// Object.getOwnPropertyNames 枚举对象所有的属性 :不管该内部属性能否被枚举var attributes3 = Object.getOwnPropertyNames(Person.prototype);alert(attributes3);开发时常用的方式
//属性都放到类里function Person(name , age , friends , job){ this.name = name ; this.age = age ; this.friends = friends ; this.job = job ;}//方法都放到原型Person.prototype = { constructor: Person , sayName : function(){ alert(this.name); }};特点:即继承了父类的模版,又继承父类的原型对象 缺点:……
// 父类function Person(name, age){ this.name = name ; this.age = age ;}Person.prototype.id = 10 ;// 子类function Boy(sex){ this.sex = sex ; }//继承Boy.prototype = new Person('z3');var b = new Boy();alert(b.name);alert(b.id);只继承模版,不继承原型对象
// 父类function Person(name, age){ this.name = name ; this.age = age ;}Person.prototype.id = 10 ;// 子类function Boy(name , age , sex){ Person.call(this,name,age); this.sex = sex;}var b = new Boy('张三' , 20 , '男');alert(b.name);alert(b.sex);alert(b.id); //父类的原型对象并没有继承原型继承+借用构造函数继承
缺点:继承了父类的2次模版
// 父类function Person(name, age){ this.name = name ; this.age = age ;}Person.prototype.id = 10 ;Person.prototype.sayName = function(){alert(this.name);};// 子类function Boy(name , age , sex){ Person.call(this,name,age); // 借用构造函数继承父类的模版 this.sex = sex ; } // 原型继承Boy.prototype = new Person(); //继承父类的模版和原型对象var b = new Boy('李四' , 20 , '男');alert(b.name);alert(b.sex);b.sayName();混合继承改进
//继承1次父类的模版 继承一次父类的原型对象function extend(sub ,sup){ // 目的:实现只继承父类的原型对象 var F = new Function(); // 创建一个空函数 目的:空函数进行中转 F.prototype = sup.prototype; // 实现空函数的原型对象和超类的原型对象转换 sub.prototype = new F(); // 原型继承 sub.prototype.constructor = sub ; // 还原子类的构造器 //保存父类的原型对象: 一方面方便解耦 另一方面方便获得父类的原型对象 sub.superClass = sup.prototype; //自定义一个子类的静态属性 接受父类的原型对象 //判断父类的原型对象的构造器(加保险) if(sup.prototype.constructor == Object.prototype.constructor){ sup.prototype.constructor = sup ; }}//父类function Person( name , age){ this.name = name ; this.age = age ; }Person.prototype = { constructor: Person , sayHello: function(){ alert('hello world!'); }};//子类function Boy(name , age , sex){ //借用构造函数继承 只复制了父类的模版 Boy.superClass.constructor.call(this , name , age); this.sex = sex ;}extend(Boy , Person);// 给子类加了一个 原型对象的方法Boy.prototype.sayHello = function(){ alert('hi javascript!');}新闻热点
疑难解答