简体   繁体   English

将新方法添加到javascript中的对象有什么区别?

[英]what 's the difference about adding the new method to an object in javascript?

There are two way to add the method to an object in javascript, What 's the differnce about the below ways? 有两种方法可以将方法添加到javascript中的对象中,以下方法有何区别?

1 1个

var o = new Object(); o.method = function(){}

2 2

var o = new Object(); o.prototype.method = function(){}

The "prototype" is global. “原型”是全局的。 It can be used by all objects. 可以被所有对象使用。 You can even override or remove standard methods. 您甚至可以覆盖或删除标准方法。

if you are defining a new method for an object, so its local and is used only with this instance of the object 如果您正在为对象定义新方法,那么它的local和仅与该对象的此实例一起使用

on the other hand if you define it within the prototype scope then its global and the method is defined for all instances 另一方面,如果您在原型范围内定义它,那么将为所有实例定义它的全局和方法

There's a couple things that you have to get straight first about prototypes. 关于原型,您必须首先了解几件事。 What I write here is a rough overview. 我在这里写的是一个粗略的概述。 There a few other things that go into how prototypes work, but I'm trying to keep this simple and to the basics. 原型的工作方式还有其他一些问题,但是我试图将其简化和保持基础。

This blog post has some good details about what I'm saying: http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/ 这篇博客文章详细介绍了我的意思: http : //javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/

Only functions have .prototype properties. 只有函数具有.prototype属性。 Instances have .constructor properties. 实例具有.constructor属性。 In your example, 'o' is an instance of Object. 在您的示例中,“ o”是Object的实例。

All constructors are functions. 所有构造函数都是函数。

Depending on the browser you're using, an instance will have a prototype property like: 根据您使用的浏览器,实例将具有如下原型属性:

.__proto__ 

This is what you normally think of as the .prototype property on a function. 这就是您通常认为的函数的.prototype属性。 Most (all?) versions of IE do not provide "_ _ proto _ _" (extra spaces for SO formatting) properties on instances. IE的大多数(全部?)版本在实例上提供“ _ _ proto _ _”(用于SO格式化的多余空间)属性。 Browsers that support ECMA5 have a method called getPrototypeOf() which returns the prototype of an instance. 支持ECMA5的浏览器有一个名为getPrototypeOf()的方法,该方法返回实例的原型。

Since constructors are functions, they have .prototype properties. 由于构造函数是函数,因此它们具有.prototype属性。 So o.constructor.prototype is a good way to access the prototype in a friendly cross-browser way. 因此,o.constructor.prototype是一种以友好的跨浏览器方式访问原型的好方法。 There are caveats to this, as it can be overwritten. 有一些注意事项,因为它可能会被覆盖。 See the blog post for more info. 有关更多信息,请参见博客文章。

So the first thing we need to do is to re-write your code in such a way that the syntax is actually working like we are expecting. 因此,我们需要做的第一件事就是以一种使语法实际上像我们期望的那样工作的方式来重新编写代码。 I'm changing the variable names to be a little clearer from this point on: 从现在开始,我将变量名称更改为更加清晰:

var foo = new Object(); //a new instance of Object
foo.foo_method = function(){ console.log('foo'); } //add a function called 'foo_method' to our instance

var bar = new Object(); // a new instance of Object
bar.constructor.prototype.bar_method = function(){ console.log('bar'); } //add a function called 'bar_method' to the Object prototype


foo.foo_method(); //logs "foo"
foo.bar_method(); //logs "bar"

bar.bar_method(); //logs "bar"

try{
    bar.foo_method(); 
}catch(err){
   console.log(err); //Uncaught TypeError: Object #<Object> has no method 'foo_method' or "TypeError"
}

//Alternate way to add methods to prototypes. Only works in non-IE browsers.
bar.__proto__.woohoo_method = function(){ console.log('woohoo'); }

foo.woohoo_method(); //logs "woohoo"
bar.woohoo_method(); //logs "woohoo"​​​​​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有关在JavaScript中向对象添加方法的说明? - Clarification about adding a method to an object in JavaScript? Javascript-此对象定义符号有什么区别? - Javascript - What's the difference with this object definiton notations? JavaScript中的“节点”和“对象”有什么区别? - What's the difference between “node” and “object” in JavaScript? Go和Javascript关于0xFFFFFFFF ^ 97有什么区别 - What's the difference between Go and Javascript about 0xFFFFFFFF ^ 97 javascript 中的 object 方法和缩短的 object 方法语法有什么区别? - What is the difference between object method and shortened object method syntax in javascript? JavaScript中“ new Class”和“ new Class()”有什么区别 - What's the difference between “new Class” and “new Class()” in javascript 关于 Object.create() 的 output 和 javascript 中的“新”方法的问题 - question about the output of Object.create() and "new" method in javascript 关于 JavaScript 对象的方法不可枚举的困惑 - Confusion about JavaScript Object's method being non-enumerable JavaScript 对象和 OO/UML/Java 对象有什么区别? - What's the difference between a JavaScript object and an OO/UML/Java object? Javascript对象和JSON对象之间有什么区别 - What's the difference between Javascript Object and JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM