sdn all cal func images function typeof net 调用父类
原链接:http://blog.csdn.net/hongse_zxl/article/details/44622997
- //定义父类Person,构造函数内有两个属性name和age,原型对象内定义了sayName方法
- function Person(name, age) {
- this.name = name;
- this.age = age;
- }
- Person.prototype.sayName = function() {
- alert(this.name);
- }
- //定义子类Man,让其模拟继承父类
- Personfunction Man(name, age) {
- Person.call(this, name, age); //子类中调用父类的构造函数
- this.gender = "male"; //子类中定义个新属性gender
- }
- Man.prototype = new Person(); //继承是通过创建父类的原型对象,并将子类的prototype指针指向该原型对象来实现的。
- Man.prototype.constructor = Person;
- Man.prototype.sayGender = function() {
- alert(this.gender);
- };
- var m1 = new Man("Jack", 32);
- m1.sayName(); //Jack
- m1.sayGender(); //male
- var m2 = new Man("Zhang", 30);
- m2.sayName(); //Zhang
- m2.sayGender(); //male
- alert(m1 instanceof Object); //true,显然创建的实例对象,既是Object,也是Person,也是Man
- alert(m1 instanceof Person); //true
- alert(m1 instanceof Man); //true
- alert(Object.prototype.isPrototypeOf(m1)); //true
- alert(Person.prototype.isPrototypeOf(m1)); //true
- alert(Man.prototype.isPrototypeOf(m1)); //true
理解JS 原型链 ( 一 )
sdn all cal func images function typeof net 调用父类
原文:http://www.cnblogs.com/conserdao/p/7875404.html
来源: http://www.bubuko.com/infodetail-2403621.html