简体   繁体   English

Javascript Closures vs Object.createProperty

[英]Javascript Closures vs Object.createProperty

My question is as follows : In most or all tutorials that I read about closures their first and foremost asset is described as them being able to define private members . 我的问题如下:在我阅读关于闭包的大多数或所有教程中,他们的第一个和最重要的资产被描述为能够定义私有成员。 For example , from the fabulous nettuts website : http://net.tutsplus.com/tutorials/javascript-ajax/digging-into-design-patterns-in-javascript/ . 例如,从神话般的nettuts网站: http ://net.tutsplus.com/tutorials/javascript-ajax/digging-into-design-patterns-in-javascript/。 My question is as follows - Why would you choose to create objects with closures , with their somewhat unnatural syntax , when you can use Object.definteProperty ? 我的问题如下 - 当你可以使用Object.definteProperty时,为什么你会选择使用闭包创建具有某种不自然语法的对象? eg 例如

var o = {}; // Creates a new object

 // Example of an object property added with defineProperty with a data property descriptor
Object.defineProperty(o, "a", {value : 37,
                           writable : false,
                           enumerable : true,
                           configurable : true});
 // 'a' property exists in the o object and its value is 37

I must admit both ways are much longer than traditional OOP languages , but isn't the second way more natural ? 我必须承认这两种方式都比传统的OOP语言长得多,但第二种方式不是更自然吗? Why then are closures so popular , what other assets do they have and what is the difference between creating an object with a closure or with the way I just described? 那么为什么闭包如此受欢迎,它们有什么其他资产以及用闭包创建对象或者我刚刚描述的方式有什么区别?

Object.defineProperty still defines public properties, while closures allow privacy (you should not talk of private "members", it's just local variables). Object.defineProperty仍定义公共属性,而闭包允许隐私(你不应该谈论私有“成员”,它只是局部变量)。

Your example, using defineProperty to define a enumerable, writable and configurable data property can (and should) be abbreviated by the default property assignment (which works in older browsers as well): 您的示例,使用defineProperty定义可枚举,可写和可配置的数据属性可以(并且应该)缩写为默认属性赋值(也适用于旧版浏览器):

o.a = 37;

Not all objects need to be created with closures (and in your example I can't think of any application), but you might want to learn How do JavaScript closures work? 并非所有对象都需要使用闭包创建(在您的示例中我无法想到任何应用程序),但您可能想学习JavaScript闭包如何工作? .

It is a matter of syntax preference and browser compatibility. 这是语法首选项和浏览器兼容性的问题。 One big gotcha, is if you want browser compatible code, the defineProperty method is only available in IE 9+ for non-dom objects. 一个很大的问题是,如果你想要与浏览器兼容的代码,那么defineProperty方法仅适用于非dom对象的IE 9+。

As far as syntax goes, you can see that you loose a bit of what I call "visual encapsulation" when using defineProperty . 就语法而言,你可以看到在使用defineProperty时你松了一点我称之为“视觉封装”的defineProperty

Each way has its own benefits, but both lack the simplicity of a language which supports privacy more directly. 每种方式都有其自身的优点,但两者都缺乏更直接支持隐私的语言的简单性。

Note: 注意:

  • I am assuming by priavate you mean you are setting the writable property to false. 我假设priavate你的意思是你将可写属性设置为false。

Reference 参考

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM