zoukankan      html  css  js  c++  java
  • js之面向对象

    好文章链接:http://www.cnblogs.com/asqq/archive/2013/02/01/3194993.html

    好的对象声明方式:

      4、组合构造函数及原型模式

          目前最为常用的定义类型方式,是组合构造函数模式与原型模式。构造函数模式用于定义实例的属性,而原型模式用于定义方法和共享的属性。结果,每个实例都会 有自己的一份实例属性的副本,但同时又共享着对方方法的引用,最大限度的节约内存。此外,组合模式还支持向构造函数传递参数,可谓是集两家之所长。

    function Person(name, age, job) {
        this.name = name;
        this.age = age;
        this.job = job;
        this.lessons = ['Math', 'Physics'];
    }
    Person.prototype = {
        constructor: Person,//原型字面量方式会将对象的constructor变为Object,此外强制指回Person
        getName: function () {
            return this.name;
        }
    }
    var person1 = new Person('Jack', 19, 'SoftWare Engneer');
    person1.lessons.push('Biology');
    var person2 = new Person('Lily', 39, 'Mechanical Engneer');
    alert(person1.lessons);//Math,Physics,Biology
    alert(person2.lessons);//Math,Physics
    alert(person1.getName === person2.getName);//true,//共享原型中定义方法
  • 相关阅读:
    初识ambari
    MySQL Split 函数
    行存储和列存储
    Hbase安装和错误
    mysql 常用自定义函数解析
    mysq l错误Table ‘./mysql/proc’ is marked as crashed and should be repaired
    MySql提示:The server quit without updating PID file(…)失败
    mysql 自定义函数
    hive 调优总结
    [css] line boxes
  • 原文地址:https://www.cnblogs.com/baoliwei/p/4476301.html
Copyright © 2011-2022 走看看