zoukankan      html  css  js  c++  java
  • js的构造函数和原型

    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
  • 相关阅读:
    jquery自调用匿名函数解析
    C# 分页
    C#一般处理程序获取Session
    Python全栈开发,Day12
    Python全栈开发,Day11
    Python全栈开发,Day10
    Python全栈开发,Day9
    Python全栈开发,Day8
    Python全栈开发,Day7
    Python全栈开发,Day6
  • 原文地址:https://www.cnblogs.com/xiaoxinstart/p/12782813.html
Copyright © 2011-2022 走看看