简体   繁体   English

编写符合ECMAScript5的代码(第2部分)

[英]Writing ECMAScript5 compliant code (Part 2)

I am currently learning advanced JavaScript, with an aim to build a standards compliant (HTML5, CSS3, ESv5) library. 我目前正在学习高级JavaScript,旨在构建符合标准的(HTML5,CSS3,ESv5)库。 Along my way I have already asked a couple of related questions to try and figure out where to start, what to do, what not to do, what to avoid etc. I have already begun reading the ECMA-262 (ECMAScript version 5) documentation, and have been running a few tests before I get started on development work. 在我的方式,我已经问了几个相关的问题,试图找出从哪里开始,做什么,不该做什么,避免什么等等。我已经开始阅读ECMA-262(ECMAScript第5版)文档在开始开发工作之前,我已经运行了一些测试。

Previous questions: 以前的问题:

Writing ECMAScript5 compliant code 编写符合ECMAScript5标准的代码

What's the difference between JavaScript, JScript & ECMAScript? JavaScript,JScript和ECMAScript之间有什么区别?

In my research I found out that different browsers implement the standard differently, and in that respect, they implement different objects. 在我的研究中,我发现不同的浏览器以不同的方式实现标准,并且在这方面,它们实现不同的对象。 For example, IE implements an object called ActiveXObject, but this is not the case in FireFox. 例如,IE实现了一个名为ActiveXObject的对象,但在FireFox中并非如此。 So I wrote a little test facility which determines if something is defined within the browser. 所以我编写了一个小测试工具,用于确定浏览器中是否定义了某些内容。

Consider the following which tests a few known objects (including jQuery since this is not built in). 考虑以下测试一些已知对象(包括jQuery,因为它不是内置的)。

浏览器功能测试工具

Again, I have reached a point where I am in need of help: 我再次达到了需要帮助的程度:

Questions: 问题:

  1. Given the example above, what is the difference between an object and a function? 鉴于上面的例子,对象和函数之间有什么区别?

  2. Do I write functions or objects in ES/JS? 我在ES / JS中编写函数或对象吗?

  3. Why is Object a function and not an object? 为什么Object是一个函数而不是一个对象?

  4. Is there any hierarchical structure to built in objects / functions? 内置对象/函数是否有任何层次结构?

  5. Can built in objects / functions be redefined as something entirely different? 可以将内置的对象/函数重新定义为完全不同的东西吗?

  6. Can built in objects / functions be undefined? 可以内置对象/函数吗?

  7. Can built in objects / functions be assigned new features if they do not already support them natively? 可以为内置的对象/函数分配新功能,如果它们本身不支持它们吗?

  8. If an object is defined in one browser and not another, how can I compensate for this? 如果一个对象是在一个浏览器中而不是另一个浏览器中定义的,我该如何补偿?

PS I do not want answers relating to specific implementations (JavaScript/JScript), rather answers relating to the standard (ECMAScript v5). PS我不想要与特定实现(JavaScript / JScript)相关的答案,而是与标准(ECMAScript v5)相关的答案。 Thanks in advance! 提前致谢!

Given the example above, what is the difference between an object and a function? 鉴于上面的例子,对象和函数之间有什么区别?

In Chrome, all these items are functions. 在Chrome中,所有这些项目都是功能。 In general however, a function is an object with the addition that it holds code and that you can call it. 但是,一般情况下,函数是一个对象,它还包含代码并且可以调用它。 So, you can also just add properties to functions (like jQuery does: $("selector") or $.ajax ). 所以,你也可以只为函数添加属性(比如jQuery: $("selector")$.ajax )。

Do I write functions or objects in ES/JS? 我在ES / JS中编写函数或对象吗?

Well, obviously that depends on what you code. 嗯,显然这取决于你的代码。 function() {} gives you a function; function() {}为您提供一个函数; {} gives you an object. {}为您提供了一个对象。 (Again, functions are objects in the end.) (同样,函数最终对象。)

Why is Object a function and not an object? 为什么Object是一个函数而不是一个对象?

Object is a function because you can call it, either as a constructor or not: Object是一个函数,因为您可以将其作为构造函数调用它:

Object();      // returns an empty object
new Object();  // same

Also, given that almost everything is an instance of Object , it follows that Object is a constructor and thus a function. 此外,鉴于几乎所有东西都是Object的实例,因此Object是一个构造函数,因此是一个函数。 (Note again that functions are also objects.) (再次注意,函数也是对象。)

Is there any hierarchical structure to built in objects / functions? 内置对象/函数是否有任何层次结构?

As for the ECMAScript built-in objects, there is in a sense. 至于ECMAScript内置对象,从某种意义上说。 There are constructor functions ( String ) on the global object, functions for instances ( Array.prototype.forEach ), and "static" functions ( Object.defineProperty which is meant to be used on objects, Array.isArray for arrays). 全局对象上有构造函数( String ),实例函数( Array.prototype.forEach )和“静态”函数( Object.defineProperty用于对象, Array.isArray用于数组)。

Can built in objects / functions be redefined as something entirely different? 可以将内置的对象/函数重新定义为完全不同的东西吗?

Sure, you can do Object = null . 当然,你可以做Object = null But any code relying on Object will start throwing exceptions, so it's not recommended at all. 但任何依赖于Object代码都会开始抛出异常,所以根本不建议这样做。

Can built in objects / functions be undefined? 可以内置对象/函数吗?

No, an object is not undefined by definition. 不,根据定义,对象不是未定义的。 undefined is not an object and vice-versa. undefined不是对象,反之亦然。 This holds for any object. 这适用于任何对象。

Can built in objects / functions be assigned new features if they do not already support them natively? 可以为内置的对象/函数分配新功能,如果它们本身不支持它们吗?

Yes, if eg Array.prototype.forEach does not exist, you could set it yourself. 是的,如果例如Array.prototype.forEach不存在,您可以自己设置它。 But it should be noted that such functions turn up in for(var key in arr) loops which again can cause code to behave differently. 但应该注意的是,这些函数会出现for(var key in arr)循环,这又会导致代码表现不同。 This can be solved using Object.defineProperty by using {enumerable: false} . 这可以通过使用{enumerable: false}使用Object.defineProperty来解决。 But there is another caveat: the function is shared across the whole environment (eg the current page). 但还有另一个警告:该功能在整个环境中共享(例如当前页面)。 If other code is also setting them you're experiencing collisions. 如果其他代码也在设置它们,则表示您遇到了冲突。

If an object is defined in one browser and not another, how can I compensate for this? 如果一个对象是在一个浏览器中而不是另一个浏览器中定义的,我该如何补偿?

You can "shim" such functions. 你可以“填充”这些功能。 For eg ES5 functions such as Array.prototype.forEach there are shims available which make them available on older browsers as well. 对于例如ES5函数,例如Array.prototype.forEach ,可以使用Array.prototype.forEach ,这些Array.prototype.forEach也可以在旧版浏览器上使用。 Underscore.js may be a good example. Underscore.js可能就是一个很好的例子。

Given the example above, what is the difference between an object and a function? 鉴于上面的例子,对象和函数之间有什么区别?

A function is just an object which is callable . 函数只是一个可调用对象 However, I guess you ask for the types of host objects ( Node , HTMLCollection etc): Their behaviour is implementation-dependent ("not ecmascript-native") - you can't rely on anything. 但是,我猜你要求主机对象的类型( NodeHTMLCollection等):它们的行为依赖于实现(“不是ecmascript-native”) - 你不能依赖任何东西。

Do I write functions or objects in ES/JS? 我在ES / JS中编写函数或对象吗?

Huh? 咦? You write code , which can be interpreted. 您编写可以解释的代码

Why is Object a function and not an object? 为什么Object是一个函数而不是一个对象?

Object is the native object constructor , and therefore a function (and also an Object). Object是本机对象构造函数 ,因此是一个函数(也是一个Object)。

Is there any hierarchical structure to built in objects / functions? 内置对象/函数是否有任何层次结构?

Do you ask for " Everything is an Object "? 你问“ 一切都是对象 ”吗? If you ask for the structure of DOM interfaces: They are implementation-dependent host objects again, but most implementors have a inheritance system based on the DOM specification . 如果你要求DOM接口的结构:它们又是依赖于实现的主机对象,但是大多数实现者都有一个基于DOM规范的继承系统。

Can built in objects / functions be redefined as something entirely different? 可以将内置的对象/函数重新定义为完全不同的东西吗? Can built in objects / functions be undefined? 可以内置对象/函数吗?

No. You can overwrite the global variables pointing to them (the properties of the global object), but every instance will nevertheless be constructed from the native (then [ nearly ] unaccessible) constructors. 不可以。您可以覆盖指向它们的全局变量(全局对象的属性),但是每个实例仍然可以从本机(然后[ 几乎 ]不可访问的)构造函数构造。

Can built in objects / functions be assigned new features if they do not already support them natively? 可以为内置的对象/函数分配新功能,如果它们本身不支持它们吗? If an object is defined in one browser and not another, how can I compensate for this? 如果一个对象是在一个浏览器中而不是另一个浏览器中定义的,我该如何补偿?

Yes, you can extend the native objects and their prototypes. 是的,您可以扩展本机对象及其原型。 But watch out for host objects, they might not like it . 但要注意主机对象,他们可能不喜欢它 If an object is defined only in certain environments, you can easily test for its existance and possibly shim it ( es5 , html5 ). 如果仅在某些环境中定义对象,则可以轻松测试其存在并可能对其进行填充es5html5 )。

As part of my research into ECMAScript / JavaScript, I have found the following resource which provides a lot of information regarding the JS DOM. 作为我对ECMAScript / JavaScript的研究的一部分,我发现了以下资源,它提供了有关JS DOM的大量信息。

http://krook.org/jsdom/index-all.html http://krook.org/jsdom/index-all.html

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

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