美文网首页
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>

相关文章

  • ajax

    1. 面向对象 javascript 具有面向过程,面向对象,函数式编程的特点 javascript 重要 原型/...

  • javascript 面向对象编程

    引自:阮一峰的博客Javascript面向对象编程(一):封装Javascript面向对象编程(二):构造函数的继...

  • JS创建对象方案(一)

    5.1 JavaScript的面向对象 JavaScript其实支持多种编程范式的,包括函数式编程和面向对象编程:...

  • 构造函数和原型对象

    javascript使用构造函数和原型对象来进行面向对象编程 构造函数 在 JavaScript 中,构造器其实就...

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

  • JavaScript 面向对象编程

    JavaScript 快速入门 面向对象编程创建对象构造函数忘记写new怎么办?原型继承class继承 面向对象编...

  • 2018-11-23 面向对象4 ES6

    面向对象补充: JavaScript 通过构造函数生成新对象,因此构造函数可以视为对象的模板。实例对象的属性和方法...

  • 2018-11-22

    JavaScript的面向对象是基于constructor(构造函数)与prototype(原型链)的。 构造函数...

  • 22

    JavaScript的面向对象是基于constructor(构造函数)与prototype(原型链)的。 构造函数...

  • 面向对象2

    JavaScript的面向对象是基于constructor(构造函数)与prototype(原型链)的。 构造函数...

网友评论

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

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