javascript 继承机制
2014-04-24 18:50
357 查看
<script type="text/javascript">
/*对象继承机制*/
/*----------------------------构造函数绑定----------------------------*/
function Animal(){
this.species="动物";
}
function Cat(name,color){
Animal.apply(this,arguments);
this.name=name;
this.color=color;
}
/*var cat1=new Cat("猫","yellow");
console.log(cat1.species+'----------构造函数绑定');*/
/*----------------------------prototype 对象继承---------------------------*/
Cat.prototype=new Animal();
Cat.prototype.constructor=Cat;
console.log(Cat.prototype.constructor==Cat);
var cat2=new Cat('dog','red');
console.log(cat2.constructor);/*每个实例也有也有一constructor 属性,默认调用prototype对象的contructor属性。*/
console.log(cat2.species);
/*----------------------------prototype 对象继承2---------------------------*/
function Animal2(){
};
function Dog(name){
this.name=name;
}
Animal2.prototype.species='动物 2';
Dog.prototype=Animal2.prototype;
Dog.prototype.constructor=Dog;
var dog=new Dog("dog1");
console.log(dog.constructor);
console.log(dog.species+'------------');
/*---------------------------- 空对象作为介质 对象继承---------------------------*/
function Animal3(){
};
Animal3.prototype.species='动物 3';
function F(){};
F.prototype=Animal3.prototype;
Dog.prototype=new F();
Dog.prototype.constructor=Dog;
var dog2=new Dog('dog2');
console.log(dog2.species);
/*可以将上面方法封装成一个函数*/
function extend(Parent,Child){
var F=function(){};
F.prototype=Parent.prototype;
Child.prototype=new F();
Child.prototype.constructor=Child;
Child.uber=Parent.prototype;
};
extend(Animal3,Dog);
var dog3=new Dog('dog3');
console.log(dog3.species+'-----extend 方法实现');
/*对象继承机制*/
/*----------------------------构造函数绑定----------------------------*/
function Animal(){
this.species="动物";
}
function Cat(name,color){
Animal.apply(this,arguments);
this.name=name;
this.color=color;
}
/*var cat1=new Cat("猫","yellow");
console.log(cat1.species+'----------构造函数绑定');*/
/*----------------------------prototype 对象继承---------------------------*/
Cat.prototype=new Animal();
Cat.prototype.constructor=Cat;
console.log(Cat.prototype.constructor==Cat);
var cat2=new Cat('dog','red');
console.log(cat2.constructor);/*每个实例也有也有一constructor 属性,默认调用prototype对象的contructor属性。*/
console.log(cat2.species);
/*----------------------------prototype 对象继承2---------------------------*/
function Animal2(){
};
function Dog(name){
this.name=name;
}
Animal2.prototype.species='动物 2';
Dog.prototype=Animal2.prototype;
Dog.prototype.constructor=Dog;
var dog=new Dog("dog1");
console.log(dog.constructor);
console.log(dog.species+'------------');
/*---------------------------- 空对象作为介质 对象继承---------------------------*/
function Animal3(){
};
Animal3.prototype.species='动物 3';
function F(){};
F.prototype=Animal3.prototype;
Dog.prototype=new F();
Dog.prototype.constructor=Dog;
var dog2=new Dog('dog2');
console.log(dog2.species);
/*可以将上面方法封装成一个函数*/
function extend(Parent,Child){
var F=function(){};
F.prototype=Parent.prototype;
Child.prototype=new F();
Child.prototype.constructor=Child;
Child.uber=Parent.prototype;
};
extend(Animal3,Dog);
var dog3=new Dog('dog3');
console.log(dog3.species+'-----extend 方法实现');
相关文章推荐
- Javascript继承机制的设计思想
- 重温Javascript继承机制
- JavaScript如何实现面向对象和继承机制?
- Javascript 继承机制试用
- 重温Javascript继承机制
- javascript 之 prototype继承机制
- javascript的继承机制
- javascript的继承机制
- 重温Javascript继承机制(转载)
- Javascript继承机制的设计思想
- Javascript继承机制
- (非常经典)Javascript继承机制的设计思想
- Javascript继承机制的设计思想
- Javascript继承机制
- JavaScript继承机制模拟实现
- JavaScript中的继承机制
- 基于JavaScript实现继承机制之调用call()与apply()的方法详解
- Javascript继承机制总结
- JavaScript 的继承机制(图文)
- 转载:对javascript的继承机制prototype的理解 (~很明了的阐述)