zoukankan      html  css  js  c++  java
  • 面向对象(三)-读书笔记

    最后介绍一种集大成者-组合使用构造函数模式和原型模式。

    function Person(name, age, job){
        this.name = name;
        this.age = age;
        this.job = job;
        this.friends = ["Shelby", "Court"];
    }
    Person.prototype = {
        constructor : Person,
        sayName : function(){
            console.log(this.name);
        }
    }
    var person1 = new Person("Nicholas", 29, "Software Engineer");
    var person2 = new Person("Greg", 27, "Doctor");
    person1.friends.push("Van");
    console.log(person1.friends); //"Shelby,Count,Van"
    console.log(person2.friends); //"Shelby,Count"
    console.log(person1.friends === person2.friends); //false
    console.log(person1.sayName === person2.sayName); //true

    实例属性都是在构造函数中定义的,而由所有实例共享的属性constructor 和方法sayName()则是在原型中定义的。而修改了person1.friends(向其中添加一个新字符串),并不会影响到person2.friends,因为它们分别引用了不同的数组。

    这种构造函数与原型混成的模式,是目前在ECMAScript 中使用最广泛、认同度最高的一种创建自定义类型的方法。可以说,这是用来定义引用类型的一种默认模式。

    参考资料

    《javascript高级程序设计(第3版)》第6章 面向对象的程序设计

  • 相关阅读:
    qemu-kvm虚拟化——内存
    Virtualization and Performance: Understanding VM Exits
    Linux日志文件
    Linux那些让你虎躯一震的命令
    Linux命令——watch
    Linux kernel buffer ring
    Linux命令——dmesg
    Linux命令——systemctl
    Linux命令——taskset
    /sys 和 /dev 区别
  • 原文地址:https://www.cnblogs.com/winteronlyme/p/6709354.html
Copyright © 2011-2022 走看看