ecmascript-5

Is there any practical use of redefining Math.constructor in JavaScript/ActionScript?

╄→尐↘猪︶ㄣ 提交于 2019-12-06 09:06:22
问题 The Math object does not have a prototype property, but does have a constructor property. Is there any case in which redefining the constructor would be useful? 回答1: MDN says: Unlike the other global objects, Math is not a constructor. All properties and methods of Math are static . In other languages, when a class is static, you can directly use its properties and methods without creating an instance of that class ( an object ). If Math constructor is used, there is no native type to support

ECMAScript5 deep copy of object and arrays

可紊 提交于 2019-12-06 07:52:47
问题 I'd hope to find an example code to do a deep copying of objects in ECMAScript5. The copying should be able to clone Nested objects Nested arrays Nested objects in arrays (clone each array item individually) Note: jQuery.extend() does not seem to handle case 3). Also, I'd hope to do this in clean ECMAScript. Quick googling did not bring up any worthy implementations. 回答1: I finally settled to jQuery.extend() as I couldn't find other good implementations http://api.jquery.com/jQuery.extend/

Does Object.keys() work in Internet Explorer 9 for built-in objects?

試著忘記壹切 提交于 2019-12-06 07:37:52
问题 The Object.keys() method works fine for me with code like this: var foo = {foo: 1, bar: 2}; console.log(Object.keys(foo).length); However, Object.keys() returns a zero-length array for built-in objects with code like this: <!doctype html> <html> <head> <title>Object.keys()</title> </head> <body> <script type="text/javascript"> console.log(Object.keys(window.document).length); </script> </body> </html> Am I missing something? I'm using Internet Explorer 9.0.8112.16421. Postscript: I'm still

Object.create instead of Constructors for inheritance

谁说我不能喝 提交于 2019-12-06 04:42:21
I want to be able to learn creating Objects per the new JavaScript ECMA 5 standards, and use them in my current projects, without breaking functionality. But I see un-explainable stuff that makes me afraid Consider the following code: var foo = { oldProp: 'ECMA Script 3', newProp: 'ECMA Script 5' }; // The new way of inheritance var bar = Object.create( foo ); // and of assigning properties, if I am right var bar2 = Object.create( Object.prototype, { oldProp: { value: 'ECMA Script 3'}, newProp: { value: 'ECMA Script 5'} }); // and then bar; // console output Object {oldProp: "ECMA Script 3",

How to make sure ES3 programs will run in an ES5 engine?

旧时模样 提交于 2019-12-06 03:56:13
问题 So ECMAScript 5 introduces some incompatibilities with ECMAScript 3. Example : Many articles have been written stating that this === null || this === undefined is possible in ES5 strict mode : "use strict"; (function () { alert(this); // null }).call(null); But, what the standard really suggests is that ES5 engines also allow this in non-strict mode : 15.3.4.3 ... The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null

Why variable object was changed to lexical environment in ES5?

╄→尐↘猪︶ㄣ 提交于 2019-12-06 03:33:24
问题 ES5 changed variable object(VO) to lexical environment. What's the motivation of such change since VO is already very obvious as perception? 回答1: I think variable objects are more analogous to environment records. An Environment Record records the identifier bindings that are created within the scope of its associated Lexical Environment. In ES5 there are two different kinds of environment records: Declarative environment records are used to define the effect of ECMAScript language syntactic

In JavaScript is Object.keys().forEach() less memory efficient than a simple for…in loop?

纵然是瞬间 提交于 2019-12-05 23:54:18
Imagine you have a very large JS object, containing millions of key/value pairs, and you need to iterate over them. This jsPerf example shows the main ways to do it, and outlines the speed differences. What I wonder though is: using Object.keys() would have a different impact on memory compared to the other looping methods, since it needs to create the "index" array that contains all the object keys first? Are there any optimizations in the source code that prevent this? What you're looking for is lazy iteration over the properties of an object or array. This is not possible in ES5 (thus not

Clarication needed for implementing properties with the revealing module pattern using Html5 getters and setters

纵饮孤独 提交于 2019-12-05 20:49:10
I've searched a lot for how to do properties in Javascript. Most all of the revealing module pattern I've seen has exclusively exposed functions, and from experience I know if I expose an object, I'm only really getting a copy of the value right there and then, thus simply I could have a function getMyThing() and setMyThing and expose that. However I'm wanting to expose real properties I've seen the oldschool defineGetter which I'm avoiding and the newer Object.defineProperty( which I had some real troubles with. (I could easily use it against an artitary object but not THIS inside my "module"

JSHint won't let me use 'forEach' in a 'for' loop

僤鯓⒐⒋嵵緔 提交于 2019-12-05 15:48:53
问题 I have an object with arrays as values. people = { 'steve':['foo','bar'], 'joe':['baz','boo'] } For each key, I would like to loop over the values in the corresponding array. Simple enough: for ( var person in people ) { person.forEach( function(item) { console.log(item) }) } But JSHint complains: Don't make functions within a loop. Is this truly an issue with my code? I quite like the short ES5 for loop syntax. Do I need to use the ES3 style or change my code in some other way? 回答1: There

How to get name of JavaScript Class

自闭症网瘾萝莉.ら 提交于 2019-12-05 14:30:56
Let's take the following example code: var ns = {}; // Some namespace ns.Test = function() { // Constructor of class Test }; var inst = new ns.Test(); var className = hereIsTheMagic(inst); // Must return "ns.Test" So I create a class named 'Test' in namespace 'ns' and an instance of this class named 'inst'. Now I want to find out the class name. How can I do this? Up to now I solved this problem by giving each class a string property with the class name so I could use inst.constructor.className to access the class name. But if possible I would like to stop doing this because it is pretty error