美文网首页
JavaScript深入浅出——函数面向对象

JavaScript深入浅出——函数面向对象

作者: 杀个程序猿祭天 | 来源:发表于2018-10-29 14:08 被阅读12次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    
    <body>
        
    </body>
    </html>
    <script>
       function Person(name,age){
        this.name = name;
        this.age = age;
       }
       Person.prototype.hi = function(){
        console.log("我的名字是"+this.name+",今年"+this.age);
       }
       Person.prototype.legs = 2;
       Person.prototype.arms = 2;
       Person.prototype.wall = function(){
         return this.name+",会走路"
       }
    
    
    
       function Status(name,age,className){
         Person.call(this,name,age);
         this.className = className
       }
    
       Status.prototype = Object.create(Person.prototype);
       Status.constructor = Status;
       Status.prototype.hi = function(){
        console.log("我的名字是"+this.name+",今年"+this.age+',班级是'+this.className);
       }
       Status.prototype.learn = function(subject){
        console.log("我的名字是"+this.name+",今年"+this.age+',班级是'+this.className+"学科是"+subject);
       }
       var obj = new Status('tom',22,"一班");
       console.log(obj.__proto__);
       console.log(Status.prototype);
       console.log(Status.prototype === obj.__proto__);//true
       console.log(Status.prototype.__proto__);
       console.log(Person.prototype);
       console.log(Person.prototype === Status.prototype.__proto__);//true
       console.log(Person.prototype.__proto__);
       console.log(Object.prototype);
       console.log(Person.prototype.__proto__ === Object.prototype);//true
       console.log(Object.prototype.__proto__);
    </script>
    

    相关文章

      网友评论

          本文标题:JavaScript深入浅出——函数面向对象

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