美文网首页Js让前端飞Web前端之路
JavaScript 中的面向对象与原型

JavaScript 中的面向对象与原型

作者: 爱涂鸦呀 | 来源:发表于2018-04-09 23:39 被阅读31次
jsProto.png

layout: post
title: JavaScript 中的面向对象和原型
tag: 编程语言


创建一个对象,新建属性和方法

var box = new Object();
box.name = 'Jack';
box.age = 100;
box.run = function() {
    return this.name + this.age + 'running...';
}

console.log(box.run());     // Jack100running...

重复实例化,代码冗余 --》 工厂模式

function CreatObject(name, age) {
    var obj = new Object();
    obj.name = name;
    obj.age = age;
    obj.run = function(){
        return this.name + this.age + 'running...';
    };

    return obj;
}
var box = new CreatObject('Jack', 100);
console.log(box.run());     // Jack100running...

无法确定实例所属对象 --》 构造函数

function Box(name, age) {
    this.name = name;
    this.age = age;
    this.run = function() {
        return this.name + this.age + 'running...';
    }
}

var box = new Box('Jack', 100);
console.log(box.run());     // Jack100running...

构造函数中的方法引用不唯一 --》 原型模式

function Box() {};

Box.prototype.name = 'Jack';
Box.prototype.age = 100;
Box.prototype.run = function() {
    return this.name + this.age + 'running....';
}
var box = new Box();
console.log(box.run());     // Jack100running...

为了体现封装 --》 原型创建的字面量方式


function Box() {};  // 此时 constructor 是指向 Box 的

var boxA = new Box();
console.log(boxA.constructor == Box);       // true
console.log(boxA.constructor == Object);    // false

// 字面量方式,相当于再次 `Box.prototype = {...};`,重写的原型会切断之前的原型;
// 新对象的 constructor 自然指向新对象 Object
Box.prototype = {    
    name: 'Jack',
    age: 100,
    run: function() {
        return this.name + this.age + 'running....';
    }
};

var box = new Box();
console.log(box.run());     // Jack100running...

// 字面量方式,constructor 会指向 Object
console.log(box.constructor == Box);    // false
console.log(box.constructor == Object);    // true

为了解决传参和共享 --》 构造函数+原型模式


 function Box(name, age) {  // 不共享的使用构造函数
     this.name = name;
     this.age = age;
 }
 Box.prototype = {          // 共享的使用原型模式
     constructor: Box,      // 字面量模式切断原型,直接强制指向 Box,解决问题
     run: function() {
        return this.name + this.age + 'running...';
     }
 };

 var box = new Box('Jack', 100);
 console.log(box.run());    // Jack100running...

构造函数+原型模式 很怪异 --》 动态原型模式


function Box(name, age) {
    this.name = name;
    this.age = age;

    if(typeof this.run != 'function') {     // 仅在第一次调用的时候初始化
        Box.prototype.run = function() {    // 注意,不可以使用字面量的方式重写原型,会切断实例和新原型之间的联系
            return this.name + this.age + 'running...';
        };
    
        /* Box.prototype = {                
            constructor: Box,
            run: function() {               // 外界box会读取不到哦
                return this.name + this.age + 'running...';
            }
        }; */
    }
}

var box = new Box('Jack', 100);
console.log(box.run());     // Jack100running...

相关文章

  • 1.web前端基础储备之—js的面向对象风格以及原型和原型链

    javascript是面向对象风格,基于原型的语言。 目标:了解js面向对象和原型原型链的关系 面向对象(OOP)...

  • ajax

    1. 面向对象 javascript 具有面向过程,面向对象,函数式编程的特点 javascript 重要 原型/...

  • 在nodejs中面向对象:Bearcat

    JS中的面向对象 最最最开始,我们先来说说JS中的面向对象。 原型链 参考文章:图解Javascript原型链 J...

  • JavaScript基础语法

    JavaScript简介 JavaScript是一门基于原型面向对象的语言,在JavaScript中,一切皆对象。...

  • JS 中的一些概念问题

    Q:描述 JavaScript 中的继承和原型链,并举例子。 JavaScript 是基于原型的面向对象语言,并无...

  • JavaScript 中的面向对象与原型

    layout: posttitle: JavaScript 中的面向对象和原型tag: 编程语言 创建一个对象,新...

  • 构造函数和原型对象

    javascript使用构造函数和原型对象来进行面向对象编程 构造函数 在 JavaScript 中,构造器其实就...

  • JS-Object 对象原型

    通过原型这种机制,JavaScript中的对象从其他对象继承功能特性,这种继承机制与经典的面向对象编程语言的继承机...

  • Javascript继承的原理

    JavaScript的继承是通过原型链继承,与传统面向对象的继承方式不同。 prototype与原型 我们从头开始...

  • 2018-11-22

    JavaScript的面向对象是基于constructor(构造函数)与prototype(原型链)的。 构造函数...

网友评论

    本文标题:JavaScript 中的面向对象与原型

    本文链接:https://www.haomeiwen.com/subject/ijgwhftx.html