美文网首页
OOP继承(Js)

OOP继承(Js)

作者: jasmine_jing | 来源:发表于2017-04-01 15:32 被阅读0次
//基于原型的继承
/*function Foo(){
    this.y =2;
    }
typeof Foo.prototype;//"object"
Foo.prototype.x = 1;
var obj = new Foo();
console.log(obj.x); //1
console.log(obj.y); //2*/

/*function Person(name,age){
    this.name = name;
    this.age = age;
    }
Person.prototype.hi = function(){
    console.log("Hi,my name is"+ this.name+", I am"+this.age+" years old now.");
    };
Person.prototype.legs_num = 2;
Person.prototype.arms_num = 2;
Person.prototype.walk =function(){
    console.log(this.name +"is walking...");
    };
function Student(name,age,className){
    Person.call(this,name,age);
    this.className = className;
    }   
        
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

Student.prototype.hi = function(){
    console.log("Hi,my name is " +this.name+", I am" +this.age +" years old now,and from " +this.className +".");
    }
    
Student.prototype.learn = function(subject){
    console.log(this.name +" is learning " +subject+" at " +this.className +".");
    }
var born = new Student('Bosn',23,'Class3');
born.hi();//Hi,my name is Bosn, I am23 years old now,and from Class3.
born.learn('English');//Bosn is learning English at Class3.
born.legs_num;
born.walk();//Bosnis walking...

Student.prototype.x =11;//赋值、添加时 影响已经实例过的对象
console.log(born.x);//11

Student.prototype={y:2};//直接修改赋值为新的对象时 对已经实例化过的对象没有影响,但是会影响 后续创建的实例 如下面的sunny 
console.log(born.y);//undefined
console.log(born.x);//11

var sunny = new Student('sunny',3,'Class lol');//再次实例一个新的对象
console.log(sunny.x);//undefined
console.log(sunny.y);//2*/

D@K{{WM43C04SFEKXYH0HJ7.png

![7M1J1U}$$N{]$@K(ENJY`YN.png](https://img.haomeiwen.com/i3167851/bc45bbd891271ce4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

YS8E1YJD45W$GQ$OPBIRCFU.png 8OYT9R1K~TIV(_F$R4V}9J7.png Paste_Image.png

实现继承的方法:

Paste_Image.png

OOP模拟重载

function Person(){
    var args = arguments;
    if(typeof args[0] ==='object' && args[0]){//防止参数为null 对象
        if(args[0].name){
            this.name = args[0].name;
        }
        if(args[0].age){
            this.age = args[0].age;
        }   
    }else{
        if(args[0]){
            this.name = args[0];
        }
        if(args[1]){
            this.age = args[1];
        }
    }
}
Person.prototype.toString = function(){
    return 'name ='+this.name+',age='+this.age;
}

var born = new Person('Born',23);
console.log(born.toString());//name =Born,age=23

var sunny = new Person({name:'Sunny',age:32});
console.log(sunny.toString());//name =Sunny,age=32

相关文章

  • OOP继承(Js)

    ![7M1J1U}$$N{]$@K(ENJY`YN.png](http://upload-images.jians...

  • js oop 对象的继承

    prototype JavaScript 继承机制的设计思想就是,原型对象的所有属性和方法,都能被实例对象共享。也...

  • JS面向对象整理篇一——基础概念衍生

    JS面向对象 oop 继承:实例可以继承A对象中的方法和属性,减少代码冗余 封装:对象把实现过程封装在方法中,调用...

  • OOP继承

  • OOP继承

  • JS部分

    原生JS 事件(冒泡、捕获) 变量、作用域 函数 对象 面向对象OOP(闭包、封装、继承) 正则表达式 Ajax(...

  • 继承

    继承继承是 OOP 语言中的一个最为人津津乐道的概念。许多OOP 语言都支持两种继承方式:接口继承和实现继承。接口...

  • Java面试总结

    1.什么是OOP、AOP OOP即面向对象编程OOP三大特征:封装、继承、多态OOP五大原则:单一职责原则 (Si...

  • 继承

    继承 oop(面向对象的三大特性):继承、封装、多态 单继承 class p:... p = 2...cl...

  • 2018-06-02 How to do inheritance

    JS is incredibly flexible. This is a quick recipe for OOP...

网友评论

      本文标题:OOP继承(Js)

      本文链接:https://www.haomeiwen.com/subject/fnviottx.html