对象与构造函数之间的差异和在javascript中的闭包 [英] the difference between object with constructor and closure in javascript

查看:120
本文介绍了对象与构造函数之间的差异和在javascript中的闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript的新手,我现在对两个概念感到困惑:object with constructor and prototype,and closure。

I am new to JavaScript, and I am now confused by the two conceptions: object with constructor and prototype, and closure.

这里,closure意味着嵌套在其中的其他函数及其环境的函数。

Here, closure means function with other functions nested in it, and its environment.

从某个角度看,两个概念似乎是类似的:

I feel that these two conceptions seems to be similar from a certain point of view:

它们都有变量,这与其他语言中的字段相似。在闭包中,它被称为环境,我认为这是一种值和某些变量之间的约束。

they both have variables, which is like fields in other languages. In closure, it is called environment, which I think is a kind of binding between values and certain variables.

此外,他们都有方法。在闭包中,我认为内部嵌套函数就像这样。

Moreover, they both have "methods". In closure, I think the inner nested functions act just like this.

那么,这两个概念之间的本质区别是什么?或者,记住数据的两种方法如何不同?

So, what is the essential difference between these two conceptions? Or, how the two approaches to remembering data differ?

如果我在上述这些概念中犯了一些错误,请纠正我,谢谢。

If I made some errors in these conceptions above, please correct me, thank you.

推荐答案

很好,闭包没有方法,因为它不是一个具体的对象 - 它更多的一个概念。使用函数来实现闭包,函数本身可以有方法,但是你不会将闭包描述为具有方法。正如你所说,闭包是一个函数加上它的环境信息。

Well, a closure doesn't have 'methods' as such, as it isn't really a concrete object - its more of a concept. Closures are implimented using functions, and functions themselves can have methods, but you wouldn't describe a closure as having methods. As you say, a closure is a function plus information about its environment.

另一方面,原型只是一个基本的JavaScript对象,所以它的正确说,原型有方法。

On the other hand a prototype is just a basic JavaScript object, so its correct to say that a prototype has methods.

这篇关于对象与构造函数之间的差异和在javascript中的闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆