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

2018-10-29  本文已影响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>
上一篇下一篇

猜你喜欢

热点阅读