JavaScript is a class-less language, however classes can be simulated using functions.

eg:

// A car 'class'
function Car(model) {
this.model = model;
this.color = 'silver';
this.year = '2012';
this.getInfo = function () {
return this.model + ' ' + this.year;
}
}

We can then instantiate the object using the Car constructor we defined above like this:

var myCar = new Car('ford');
myCar.year = '2010';
console.log(myCar.getInfo());

另一个例子

var Person = function (name) {
this.name = name;
this.say = function () {
return "I am " + this.name;
};
}; // use the class
var adam = new Person("Adam");
adam.say(); // "I am Adam"

上述关于类函数的做法性能不佳,原因如下:

any time you call new Person() a new function is created in memory.

This is obviously inefficient, because the say() method doesn’t change from one instance to the next.

(每次new对象的时候会在内存中新建一个函数)

The better option is to add the method to the prototype of Person 

(比较好的方法是把方法增加到Person对象的prototype)

Person.prototype.say = function () {
return "I am " + this.name;
};

just remember that reusable members, such as methods, should go to the prototype 

(关于JS类必须要记住的是,可重用的成员,例如方法必须放到对象的prototype)

Javascript Design Patterns - Js Class的更多相关文章

  1. AMD - Learning JavaScript Design Patterns [Book] - O'Reilly

    AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...

  2. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  3. Learning JavaScript Design Patterns The Observer Pattern

    The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...

  4. Learning JavaScript Design Patterns The Singleton Pattern

    The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...

  5. JavaScript Design Patterns: Mediator

    The Mediator Design Pattern The Mediator is a behavioral design pattern in which objects, instead of ...

  6. Learning JavaScript Design Patterns The Constructor Pattern

    In classical object-oriented programming languages, a constructor is a special method used to initia ...

  7. javascript design patterns

    http://jsdesignpatterns.com/ http://www.joezimjs.com/tag/design-patterns/ http://codecube.net/#archi ...

  8. Design Patterns All in One (JavaScript Version)

    Design Patterns All in One (JavaScript Version) JavaScript 设计模式 JavaScript 数据结构 23种设计模式分为 3 大类: 创建型模 ...

  9. javascript / PHP [Design Patterns - Facade Pattern]

    This pattern involves a single class which provides simplified methods required by client and delega ...

随机推荐

  1. jquery实现点击按钮滑动到指定位置

    <body> <script type="text/javascript"> function click_scroll() { var scroll_of ...

  2. (function(){})()这个是什么?有不明觉厉的感觉么?

    今天在RunJs上看到一个人分享的一个jquery代码,写的是jquery弹性滑动效果.不过,看着看着,发现一句代码(function{})(),突然有种不明觉厉的感觉. 事实上,只是因为我们没有用过 ...

  3. 多线程监控文件夹,FlieSystemWatcher,并使用共享函数

    发表于: 2011-01-06 09:55:47   C# code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  4. Mem Cgroup目录无法清理问题分析

    http://blogs.360.cn/360xitong/2013/05/02/mem-cgroup%E7%9B%AE%E5%BD%95%E6%97%A0%E6%B3%95%E6%B8%85%E7% ...

  5. logback.xml_appender配置

    logback<appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的组件. < ...

  6. 【剑指offer 面试题12】打印1到最大的n位数

    思路: 用n位字符数组表示n位数,通过递归的方式逐层(位)遍历,递归终止时打印. #include "stdio.h" #include "string.h" ...

  7. duilib修复ActiveXUI控件bug,以支持flash透明动态背景

    转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了 防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背 ...

  8. 【转】发布python的包至pypi服务器

    [原文链接]http://yejinxin.github.io/distribute-python-packages-to-pypi-server/ 使用pip或easy_install可以管理和安装 ...

  9. cocos2d-x 详解之 CCLayer(触摸事件)

    CCLayer继承自CCNode,在CCLayer中可以实现单点触摸.多点触摸和重力感应回调3种不同形式的交互.这部分的难点在于,当存在多个层都要去接收触摸时它的响应机制是如何处理的.了解内部的处理机 ...

  10. 我喜欢的乐队-Euphoria

    来自日本的后摇乐团,001年冬天由森川裕之.佐藤昭太.木下阳辅三人于东京组建,2003年签约日本独立厂牌123Record,并发行首张EP细碟<Floral Dew>.包括EP.Singl ...