JavaScript: Class.method vs Class.prototype.method
在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的更多相关文章
- javascript Class.method vs Class.prototype.method(类方法和对象方法)
在stackoverflow上看到一个这样的提问,以下代码有什么区别? Class.method = function () { /* code */ } Class.prototype.method ...
- 去除 waring Method 'CreateNew' hides virtual method of base type 'TCustomForm'
最近整理前人的代码,有好多的hint和waring, 其中整理到Method 'CreateNew' hides virtual method of base type 'TCustomForm', ...
- Java程序猿的JavaScript学习笔记(5——prototype和Object内置方法)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- 深入了解JavaScript中基于原型(prototype)的继承机制
原型 前言 继承是面向对象编程中相当重要的一个概念,它对帮助代码复用起到了很大的作用. 正文 Brendan Eich在创建JavaScript时,没有选择当时最流行的类继承机制,而是借鉴Self,用 ...
- [JavaScript] Uncaught TypeError: Method get Set.prototype.size called on incompatible receiver
在对Set进行方法扩展的时候,无法覆盖size属性 情景:定义一个SingletonSet,继承自Set,size只能为1,并且不能add和remove //首先是extend函数 var exten ...
- 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 ...
- [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 ...
- Java设计模式系列1--原型模式(Prototype Method)
2014-02-14 11:27:33 声明:本文不仅是本人自己的成果,有些东西取自网上各位大神的思想,虽不能一一列出,但在此一并感谢! 原型模式,从名字即可看出,该模式的思想就是将一个对象作为原型, ...
- Javascript中的继承与Prototype
之前学习js仅仅是把w3school上的基本语法看了一次而已,再后来细看书的时候,书中会出现很多很多没有听过的语法,其中一个就是js的继承以及总能看到的prototype.我主要在看的两本js书是&l ...
随机推荐
- 武汉科技大学ACM:1006: 华科版C语言程序设计教程(第二版)例题4.17
Problem Description 输入一个整数,求它的素数因子.并按照格式输出. Input 一个整数n.(2<=n<=100) Output n=a*b*c*... (a,b,c为 ...
- Css预处理器实践之Sass、Less大比拼
xwei | 2012-07-07 | 网页重构 什么是CSS预处理器? Css可以让你做很多事情,但它毕竟是给浏览器认的东西,对开发者来说,Css缺乏很多特性,例如变量.常量以及一些编程语法,代码难 ...
- EDIT编辑框
编辑框 编辑框的主要作用是让用户输入文本,例如要求用户在编辑框中输入密码的文本. .基础知识 编辑框里的文本可以是单行,也可以是多行,后者的风格取值为 ES_MULTILINE.一般对于多行文本编辑框 ...
- UCOS 杂项 笔记
1. 建立任务和建立数据队列 哪个先建立? 经过试验得知,数据队列和任务的建立先后没有顺序,都可以正常运行. 2.关于主函数的面试问题. 主函数写法有: int main() 和voi ...
- 设置 cell点击 背景色
//设置 cell点击 背景色 cell.selectionStyle = UITableViewCellSelectionStyleDefault; cell.selectedBackgroundV ...
- 在网页中获取 facebook page 的内容
参考 : http://www.ibm.com/developerworks/cn/opensource/os-cn-facebookapi/ 1.首先你要有 facebook page, 内容要公开 ...
- 常见MYSQL导入导出数据命令
导出数据库: mysqldump –uuser -ppassword -hhost databasename > target_20150225.sql 打包: tar zcvf target_ ...
- 分布式文件系统 FastDFS Ceph
分布式文件系统 FastDFS Cephhttp://www.oschina.net/p/fastdfshttp://www.oschina.net/p/ceph FastDFS 的 Go 客户端 f ...
- 【转】android ddms中查看线程释疑
原文网址:http://www.mobiletrain.org/lecture/doc/android/2011-05/457.html 大家都用过ddm,如果你用ddms查看一个程序的所有线程,你会 ...
- Java如何让异常处理机制更完备规范
1)catch的Exception一定要详细的点名是某种异常而非一概而论的用Exception ex来接收所有的异常,往往不理解这点的人也不能很好的理解catch的意义到底在哪里,是对捕获的异常进行一 ...