当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS高级---原型的简单的语法

JS高级---原型的简单的语法

2019年12月28日  | 移动技术网IT编程  | 我要评论

原型的简单的语法

构造函数,通过原型添加方法,以下语法,手动修改构造器的指向

实例化对象,并初始化,调用方法

 

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>title</title>
  <script>

    function student(name, age, sex) {
      this.name = name;
      this.age = age;
      this.sex = sex;
    }

    //简单的原型写法
    student.prototype = {
      //手动修改构造器的指向
      constructor: student,
      height: "188",
      weight: "70kgs",
      study: function () {
        console.log("学习12小时");
      },
      eat: function () {
        console.log("吃午饭");
      }
    };
  //实例化对象,并初始化
    var stu = new student("段飞", 20, "男");
  //调用方法 stu.eat(); stu.study(); console.dir(stu); console.dir(student); </script> </head> <body> </body> </html>

 

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网