javascript 基础: 面向对象 继承 实现方式1 javascript
发布时间:2019-01-03 23:37:46 作者:本站编辑 来源:本站原创 浏览次数:
www.javainfo.com.cn 上干货 欢迎收藏
如有疑问 请留言 欢迎提供建议
一、Object对象 //var obj=new Object(); // // 对创建对象的函数的引用(指针) //alert(obj.constructor); //function Object() { [native code] } Object对象的属性: 2.Prototype 这个属性的功能, 1 对该函数对象的对象原型的引用。是 函数对象 的默认属性 function animal(){} alert(animal.prototype); // //对象的构造函数内容 会调用出来, 说明对象都是 object的一个子类 alert(animal.constructor); var aa = new animal(); function animal(){ } alert(aa.constructor); //实例化对象的构造函数内容 会调用出来 2 对象的共享 属性存放到代码段当中 (知识回顾,prototype可以将函数 或 属性 共享到js共享代码段内存区域) function animal(){ } animal.prototype.fly=function(){ alert("share method fly"); } var laoshu = new animal(); laoshu.fly(); //share method fly 3 可以实现继承。 function animal(){ this.name = 'animal name'; this.eat=function(type){ alert(type +' eat'); } } function Laoshu(){ } Laoshu.prototype = new animal(); var laoshu = new Laoshu(); //alert(laoshu.name); //animal name 调用父类 animal的 方法成功 //laoshu eat .......... eat 调用父类的方法成功,且成功继承下来 调用 //laoshu.eat('laoshu eat .......... '); ----------------------------------------------------------------------------------------- function animal(name){ this.name = name + ' --name--'; this.eat=function(type){ alert(type +' eat'); } } function Laoshu(name){ return new animal(name); } var laoshu = new Laoshu('xiao lao shu'); alert(laoshu.name); //xiao lao shu --name-- 属性方法完成继承 ----------------------------------------------------------------------------------------------- function animal(name){ alert(2) this.name = name + ' --name--'; this.eat=function(type){ alert(type +' eat'); } } function Laoshu(name){ alert(1); } Laoshu.prototype = new animal(name); alert(Laoshu.name); var laoshu = new Laoshu('xiao lao shu'); //2 [name = --name-- ] laoshu 1 [name = xiao lao shu --name-- + --name--] 执行顺序 alert(laoshu.name); //xiao lao shu --name-- --name-- ---------------------------------------------------------------------------------------- //对象的共有属性或方法 1 hasOwnProperty(property) 判断对象是否有某个特定的属性 function animal(name){ this.name = ' animal --name--'; this.eat=function(type){ alert(type +' eat'); } } function Laoshu(name){ return new animal(name); } var animals = new animal('sf'); var laoshu = new Laoshu('xiao lao shu'); //alert(laoshu.hasOwnProperty('name')); //true //alert(laoshu.name); // animal --name-- //alert(Laoshu.prototype.isPrototypeOf(laoshu)); //false Laoshu的原型 是否是 laoshu 的原型 //alert(animal.prototype.isPrototypeOf(laoshu)); //true //alert(animal.prototype.isPrototypeOf(animals)); //true //alert(animal.isPrototypeOf(animals)); //false //alert(Laoshu.isPrototypeOf(laoshu)); //false function tfun(){} var tt = new tfun(); //alert(tfun.prototype.isPrototypeOf(tt)); //true 简单对象的实例化 比较很快 //运算符 instanceof alert(tt instanceof tfun) //true // 这里由于老鼠的构造函数 直接返回了 animal 的实例化对象,所以这里是false alert(laoshu instanceof Laoshu); //false alert(laoshu instanceof animal); //true 老鼠实际上 是 animal 对象的一个实例 alert(animals instanceof animal); //true */ 继承的JS代码实现 1.原味继承 function animal(){ this.name = ' animal --name--'; this.eat=function(type){ alert(type +' eat'); } } function Laoshu(){ } Laoshu.prototype = new animal(); var laoshu = new Laoshu(); alert(laoshu.name); //animal --name-- //true 老鼠的实例对象 继承了 animal 对象 , 老鼠是老鼠的实例化对象 true alert(laoshu instanceof Laoshu); //true alert(laoshu instanceof animal); //true 老鼠是动物的一个实例化对象 true 2.对象冒充的形式 obj1.fun.call(obj2,参数1......) function Animal(){ this.name = ' animal '; this.eat=function(p){ alert(this.name + ' ' + p + ' eat'); } } function Laoshu(){ this.name = 'laoshu'; } var animal = new Animal(); var laoshu = new Laoshu(); //laoshu tttt eat 老鼠方法成功调用了animal的方法 ,及继承了animal的方法 //且name属性覆盖了 animal的 name属性 animal.eat.call(laoshu,'tttt'); 3.apply obj1.fun.call(obj2,[参数1,参数2....]) function Animal(name){ this.name = name + ' animal '; this.eat=function(p){ alert(this.name + ' ' + p + ' eat'); } } function Laoshu(){ Animal.apply(this,['mikeee']); } var laoshu = new Laoshu(); alert(laoshu.name); //mikeee animal laoshu.eat(333); //mikeee animal 333 eat //让对象1的方法冒充成对象2的方法。
如有疑问 请留言 欢迎提供建议
评论已有 0 条