码迷,mamicode.com
首页 > Web开发 > 详细

js的构造函数和原型

时间:2020-04-26 22:35:14      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:div   prototype   alert   type   nes   构造   his   nbsp   对象   

1.构造函数创建对象

 function Person(name,age,job) {
            this.name = name;
            this.age = age;
            this.job = job;  
            this.sayName = function () {
                alert(this.name);
            }
        }
        var person1 = new Person("cherry", 25, "Engineer");
        person1.sayName();

2.原型

  function Person(name, age, job) {

            this.name = name;
            this.age = age;
            this.job = job;
        }
        Person.prototype.sayName = function () {
            alert(this.name);
        }
        var person1 = new Person("cherry", 25, "Engineer");
        person1.sayName();

3.继承

  function Person(name, age, job) {
            this.name = name;
            this.age = age;
            this.job = job;
        }
        Person.prototype.sayName = function () {
            alert(this.name);
        }
        function Chinese() {
        }
        Chinese.prototype = new Person("cherry", 25, "Egineer");//Chinese继承自Person
        var chinese = new Chinese();
        chinese.sayName();//chinese共享Person的function

js的构造函数和原型

标签:div   prototype   alert   type   nes   构造   his   nbsp   对象   

原文地址:https://www.cnblogs.com/xiaoxinstart/p/12782813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!