问题描述
与类似但不同。以下代码来自 。他基本上定义了一个继承方法,如果它存在则遵循Object.create,否则使用构造函数和交换原型进行普通的旧Javascript继承。
Similar to, but different from this question. The code below is from JavaScript: The Definitive Guide. He's basically defining an inherit method that defers to Object.create if it exists, otherwise doing plain old Javascript inheritance using constructors and swapping prototypes around.
我的问题是,因为Object 。strike在 IE上都不存在,即使尝试使用它也有什么意义?它肯定会使代码混乱,上一个问题的一位评论者提到了Object.create 。
My question is, since Object.create doesn't exist on IE, what's the point of even trying to use it? It certainly clutters up the code, and one of the commenters on the previous question mentioned that Object.create isn't too fast.
那么尝试添加额外代码以便偶尔使用这个ECMA 5的优势是什么?可能会或可能不会比旧方式慢的功能?
So what's the advantage in trying to add extra code in order to occasionally utilize this ECMA 5 function that may or may not be slower than the "old" way of doing this?
function inherit(p) {
if (Object.create) // If Object.create() is defined...
return Object.create(p); // then just use it.
function f() {}; // Define a dummy constructor function.
f.prototype = p; // Set its prototype property to p.
return new f(); // Use f() to create an "heir" of p.
}
推荐答案
速度差异不是很大值得注意的是,因为天生你可能不会创造太多的物体(数百甚至数千不是我所说的很多),如果你和速度是一个关键问题,你可能不会用JS编码,如果上述两种情况都不正确,那么我确信在所有流行的JS引擎的几个版本中,差异可以忽略不计(某些情况已经如此)。
The speed difference is not very noticeable, since by nature you probably won't be creating too many objects (hundreds, even thousands isn't what I call a lot), and if you are and speed is a critical issue you probably won't be coding in JS, and if both of the above aren't true, then I'm sure within a few releases of all popular JS engines the difference will be negligible (this is already the case in some).
在回答您的问题时,原因与速度无关,但因为 Object.create
的设计模式是赞成旧方法(原因和其他答案中概述的原因)。它们允许正确使用ES5属性属性(这样可以实现更具伸缩性的对象,从而实现更具伸缩性的应用程序),并且可以帮助继承层次结构。
In answer to your question, the reasons aren't speed-related, but because the design pattern of Object.create
is favoured to the old method (for the reasons outlined in that and other answers). They allow for proper utilisation of the ES5 property attributes (which make for more scalable objects, and thus more scalable apps), and can help with inheritance hierarchies.
它是正向工程。如果我们采取好吧,它没有在任何地方实施,所以让我们的脚不湿,事情会变得非常缓慢。相反,早期和雄心勃勃的采用有助于行业向前发展,帮助业务决策者支持新技术,帮助开发人员改进和完善新想法和支持框架。我是早期(但预防性和后向兼容)采用的倡导者,因为经验表明等待足够的人来支持技术可能会让你等待太久。对于那些不这么认为的人来说,IE6可以成为一个教训。
It's forward engineering. If we took the line of "well it isn't implemented everywhere so let's not get our feet wet", things would move very slowly. On the contrary, early and ambitious adoption helps the industry move forward, helps business decision makers support new technologies, helps developers improve and perfect new ideas and the supporting frameworks. I'm an advocate for early (but precautionary and still backward-compatible) adoption, because experience shows that waiting for enough people to support a technology can leave you waiting far too long. May IE6 be a lesson to those who think otherwise.
这篇关于使用Object.create的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!