javascript 创建对象 的几种 模式


工厂模式

能够根据接受的参数来构建一个包含所有必要信息的 Animate 对象。可以无数次地调用这个函数,而每次它都会返回一个包含三个属性一个方法的对象。工厂模式虽然解决了创建多个相似对象的问题, 但却没有解决对象识别的问题(即怎样知道一个对象的类型) 。

functionAnimate(name,age,color){

varanimate =newObject();

animate.name= name;

animate.age= age;

animate.color= color;

animate.showName=function(){

console.log("My type is "+this.name)

};

returnanimate

}

varani1 =Animate("dog",11,"red");

ani1.showName();

varani2 =Animate("cat",11,"red");

ani2.showName();

console.log(ani1.showName== ani2.showName);//false

console.log(ani1.constructor);

console.log(ani1.constructor.hasOwnProperty("age"));

随着 JavaScript的发展,又一个新模式出现了。使用构造函数的主要问题,就是每个方法都要在每个实例上重新创建一遍,那就是

构造函数模式




functionAnimated(name,age,color){

this.name= name;

this.age= age;

this.color= color;

this.showName=function(){

console.log("My type is "+this.name)

}

}

varanimate1 =newAnimated("bird",11,"red");

animate1.showName();

varanimate2 =newAnimated("monkey",11,"brown");

animate2.showName();

//获取构造函数,返回函数

console.log(animate1.constructor==Animated);//true

console.log(animate1.constructor== animate2.constructor);//true

console.log(animate1.constructor.prototype == animate2.constructor.prototype);//true

//检测对象类型

console.log(animate1instanceofAnimated);//true

console.log(animate1.showName== animate1.showName);//true

随着 JavaScript的发展,又一个新模式出现了。

原型模式




functionAnimation(name,age,color){

this.name= name;

this.age= age;

this.color= color;

}

Animation.prototype.showName=function(){

console.log("My type is "+this.name)

};

varanimation1 =newAnimation("fox",22,"oriange");

animation1.chinaName="狐狸";

animation1.showName();

varanimation2 =newAnimation("frog",22,"oriange");

animation2.showName();

console.log(animation2.constructor);

//function Animation(name,age,color){

//this.name = name;

//this.age = age;

//this.color = color;

//}

console.log(animation1.chinaName);//狐狸

console.log(animation2.chinaName);//undefined

//但与构造函数模式不同的是,新对象的这些属性和方法是由所有实例共享的。

//换句话说,animation1 和 animation2 访问的都是同一组属性和同一个 showName() 函数。

console.log(animation1.showName== animation2.showName)

//  这个是简单的写法和原型没什么区别,唯一的区别是construct 不一样

functionAnimation_sample(name,age,color){

this.name= name;

this.age= age;

this.color= color;

}

Animation_sample.prototype= {

secondName:"milk",

sayColor:function(){

console.log(this.color)

}

};

varsampleAnim1 =newAnimation_sample("follower",32,"green");

console.log(sampleAnim1.secondName);

varsampleAnim2 =newAnimation_sample("dest",32,"green");

console.log(sampleAnim2.secondName);

console.log(sampleAnim2.constructor);// function Object() { [native code] }

//我们发现 constructor 这个构造函数变成了 function Object() { [native code]

//    而我们在这里使用的语法,本质上完全重写了默认的 prototype 对象,

// 因此 constructor 属性也就变成了新对象的 constructor 属性 (指向 Object 构造函数) ,

// 不再指向 Person 函数。

//解决办法很简单,手动指定

functionAnimation_fixbug(name,age,color){

this.name= name;

this.age= age;

this.color= color;

}

Animation_fixbug.prototype= {

//添加下面这句话

constructor:Animation_fixbug,

secondName:"fishers",

sayColor:function(){

console.log(this.color)

}

};

varsampleAnimfix1 =newAnimation_fixbug("fish",32,"green");

console.log(sampleAnimfix1.secondName);

varsampleAnimfix2 =newAnimation_fixbug("fishbird",32,"green");

console.log(sampleAnimfix2.secondName);

console.log(sampleAnimfix2.constructor);// function Object() { [native code]



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容

  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,710评论 2 17
  • 前言 对象字面量以及Object构造函数均可以创建单个对象,但是这样的方式有一个致命缺点,会产生大量的重复代码,为...
    Chef_Front阅读 258评论 0 0
  • 面向对象编程 JavaScript是一种面向对象的编程语言,其基本标志就是类(class)的概念,如图所示,可以通...
    橙月_13ef阅读 135评论 0 0
  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 2,046评论 1 10
  • title: js面向对象date: 2017年8月17日 18:58:05updated: 2017年8月27日...
    lu900618阅读 555评论 0 2