在stack overflow中看到一个人回答,如下      

// constructor function
function MyClass () {
var privateVariable; // private member only available within the constructor fn this.privilegedMethod = function () { // it can access private members
//..
};
} // A 'static method', it's just like a normal function
// it has no relation with any 'MyClass' object instance
MyClass.staticMethod = function () {}; //first function MyClass.prototype.publicMethod = function () { //second function
// the 'this' keyword refers to the object instance
// you can access only 'privileged' and 'public' members
}; var myObj = new MyClass(); // new object instance myObj.publicMethod();
MyClass.staticMethod();

Yes, the first function has no relationship(有关) with an object instance of that constructor function, you can consider it like a 'static method'.

In JavaScript functions are first-class objects, that means you can treat them just like any object, in this case, you are only adding a property to the function object.

The second function, as you are extending the constructor function prototype, it will be available to all the object instances created with the new keyword, and the context within that function (the thiskeyword) will refer to the actual object instance where you call it.

简单理解:(MyClass.staticMethod ) 构造函数也是对象,可以在其上进行添加属性或方法,添加在其上的与对象实例无关

(MyClass.prototype.publicMethod)   这是扩展构造函数原型对象,用来与new关键字来创建所有对象实例。在函数中,this 指向的是实例对象。

参考地址:

JavaScript: Class.method vs Class.prototype.method的更多相关文章

  1. javascript Class.method vs Class.prototype.method(类方法和对象方法)

    在stackoverflow上看到一个这样的提问,以下代码有什么区别? Class.method = function () { /* code */ } Class.prototype.method ...

  2. 去除 waring Method 'CreateNew' hides virtual method of base type 'TCustomForm'

    最近整理前人的代码,有好多的hint和waring, 其中整理到Method 'CreateNew' hides virtual method of base type 'TCustomForm', ...

  3. Java程序猿的JavaScript学习笔记(5——prototype和Object内置方法)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  4. 深入了解JavaScript中基于原型(prototype)的继承机制

    原型 前言 继承是面向对象编程中相当重要的一个概念,它对帮助代码复用起到了很大的作用. 正文 Brendan Eich在创建JavaScript时,没有选择当时最流行的类继承机制,而是借鉴Self,用 ...

  5. [JavaScript] Uncaught TypeError: Method get Set.prototype.size called on incompatible receiver

    在对Set进行方法扩展的时候,无法覆盖size属性 情景:定义一个SingletonSet,继承自Set,size只能为1,并且不能add和remove //首先是extend函数 var exten ...

  6. A javascript library providing cross-browser, cross-site messaging/method invocation. http://easyxdm.net

    easyXDM - easy Cross-Domain Messaging easyXDM is a Javascript library that enables you as a develope ...

  7. [Javascript] Validate Data with the Every() Method

    The every method returns true or false based on whether or not every item in the array passes the co ...

  8. Java设计模式系列1--原型模式(Prototype Method)

    2014-02-14 11:27:33 声明:本文不仅是本人自己的成果,有些东西取自网上各位大神的思想,虽不能一一列出,但在此一并感谢! 原型模式,从名字即可看出,该模式的思想就是将一个对象作为原型, ...

  9. Javascript中的继承与Prototype

    之前学习js仅仅是把w3school上的基本语法看了一次而已,再后来细看书的时候,书中会出现很多很多没有听过的语法,其中一个就是js的继承以及总能看到的prototype.我主要在看的两本js书是&l ...

随机推荐

  1. java Byte.toString 方法与String.ValueOf(Byte)效率比较

    int times = 10000000; Byte[] li = new Byte[times]; for (int i = 0; i < times; i++) { li[i] = (byt ...

  2. linux初识-01简介

    什么是linux: Linux是一个自由的,免费的,源码开发的操作系统Linux的特点: 开放性.多用户,多任务,具有丰富的网络功能 可靠的系统安全 良好的可移植性 良好的用户界面(命令界面和图形界面 ...

  3. JavaScript 本地对象、内置对象、宿主对象

    首先解释下宿主环境:一般宿主环境由外壳程序创建与维护,只要能提供js引擎执行的环境都可称之为外壳程序.如:web浏览器,一些桌面应用系统等.即由web浏览器或是这些桌面应用系统早就的环境即宿主环境. ...

  4. jdk-map-hashMap

    关于java中的HashMap,我们在项目中经常使用到,但是我们的场景是否使用对了呢? 下面分为四个部分来阐述我的HashMap的理解 1.为什么要使用hashMap? 在项目中,需求的实现需要使用到 ...

  5. Linux准备——安装Ubuntu系统

    今天对于我这个试图向安装Linux方向走的童鞋来说是个值得纪念的日子——我成功的安装上了Ubuntu操作系统. 整个的过程并不复杂,主要是看了网上Linux社区的一个安装教程.开始,我还在XP下安装, ...

  6. C写的扫描器源码

    Title:C写的扫描器源码 --2010-10-27 20:02 无意间看见的一个源代码,弄回来读下. ----------------------------------------------- ...

  7. intrins.h 里面的函数都有什么,功能是什么?

    是c51中的intrins.h库 _crol_  字符循环左移 _cror_ 字符循环右移 _irol_   整数循环左移 _iror_  整数循环右移 _lrol_   长整数循环左移 _lror_ ...

  8. 无法关闭的QT程序(覆盖closeEvent,新建QProcess并脱离关系)

    做一个无法关闭的QT程序(想关闭时要在任务管理器里关闭),看似很难, 其实它并不难,只要让程序在关闭时启动它自身就可以了. 上代码: #include <QtGui> class Temp ...

  9. 神舟飞船上的计算机使用什么操作系统,为什么是自研发不是 Linux?

    中国航天用的SpaceOS主要内容是仿造美国风河系统公司的VxWorks653(653是产品名,并非版本号). 先解释为什么用这个系统不用Linux: 航天器的内存和CPU都非常弱,弱到什么程度呢:天 ...

  10. 【转】ArrayList遍历的同时删除----不错

    原文网址:http://javag.iteye.com/blog/403097 方法一 ArrayList<String> list = new ArrayList<String&g ...