English 中文(简体)
我的Object. somemasFunc = 函数 () {...} ; 或 MyObject. prototype. somemafunc = 函数 (){...) 在 javascript 中有什么区别? [重复]
原标题:What is the difference between myObject.someFunc = function(){...}; or myObject.prototype.someFunc = function(){...} in javascript? [duplicate]
  • 时间:2012-05-24 18:27:06
  •  标签:
  • javascript

假设我用刺绣写出一个物体

var myObject = {};

两者有什么区别...

myObject.someFunc = function(){...}; 

myObject.prototype.someFunc = function(){...} 

穿刺衣吗?

I am having trouble understanding the difference or if there is a difference 和how 和when to use either of these syntaxes.

当我编出这样的法典时, 似乎真的没有区别。

I am looking for both a client side (browser) 和server side (like node.js) answers.

I want to code properly 和accurately 和this is really bothering me.

最佳回答

在这种情况下:

var myObject = {};
myObject.someFunc = function(){...}; 

您正在做的只是创建一个普通对象, 该对象恰好有一个属性, 它是一个函数引用的属性。 这通常是为 < em> name mace 而做的, 也就是说, 将一个地方的一整堆函数组合在一个共同对象之下 。

但请注意, va myObject = /code> doesn t 实际上有一个 prototype , 所以您不可能有第二个示例 。

对象要有一个原型,必须是构造函数的结果,即:

function MyObject() {
    ...
}

MyObject.prototype.someFunc = function() { }

var myObject = new MyObject();
// myObject.someFunc can now be used

原型是构建符函数的属性 -- -- 不是该类的任何 < em> intentance

如果将函数放在原型中,则该函数对象只有一个实例存在,并将由该类的所有实例共享。这样,每个实例都有一个副本会提高内存效率。

所有这一切都适用,无论代码是在浏览器还是在服务器上,它仍然是同一种语言。

问题回答

基本上,这是一种静态方法(或仅附于该物体的方法(此处指 MyObject):

myObject.someFunc = function () {...};

这是附于对象原型的方法(例法):

myObject.prototype.someFunc = function () {...};

myObject. somemasFunc = 函数 () {...\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

myObject. prototype. somema Func = 函数 ()\\\\\\\\/code> 正在实际上创建一种方法,可以随我的Object 在任何地方调用, 并且每次都会改变相同的数值实例 。

myObject.prototype.someFunc = function(num){
    this.num_to_be_stored = num;
}

将会改变num_to_be_stored 而不是某些Func 的值, 而这是第一级正在发生的事情 。

编辑:抱歉清晨我并不清楚我想说什么。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.