ecmascript-5

Why is delete not allowed in Javascript5 strict mode?

懵懂的女人 提交于 2019-11-26 12:43:12
问题 I\'m fairly new to javascript, but I\'m in love with it\'s dangerously fast and loose expressiveness. That said, I noticed that apparently when operating in \"use strict\" mode, you can\'t delete objects. I\'m not a huge fan of deleting things (since, in theory, scope should take care of that anyway), but I wonder what was the motivation behind removing this feature? 回答1: The delete statement is still allowed in strict mode, but some particular uses of it are erroneous. It's only allowed for

Why were ES5 Object methods not added to Object.prototype?

て烟熏妆下的殇ゞ 提交于 2019-11-26 12:32:01
问题 ES5 added a number of methods to Object , which seem to break the semantic consistency of JavaScript. For instance, prior to this extension, the JavaScript API always revolved around operarting on the object itself; var arrayLength = [].length; var firstPosInString = \"foo\".indexOf(\"o\"); ... where as the new Object methods are like; var obj = { }; Object.defineProperty(obj, { value: \'a\', writable: false }); ... when the following would have been much more conformative: var obj = { }; obj

Why are Objects not Iterable in JavaScript?

和自甴很熟 提交于 2019-11-26 12:20:47
问题 Why are objects not iterable by default? I see questions all the time related to iterating objects, the common solution being to iterate over an object\'s properties and accessing the values within an object that way. This seems so common that it makes me wonder why objects themselves aren\'t iterable. Statements like the ES6 for...of would be nice to use for objects by default. Because these features are only available for special \"iterable objects\" which don\'t include {} objects, we have

Usage of rest parameter and spread operator in javascript

大憨熊 提交于 2019-11-26 11:04:44
问题 What\'s the usage of rest parameter that will be added in ECMAScript 6? For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element: // ES 5 store(\'Joe\', \'money\'); store(\'Jane\', \'letters\', \'certificates\'); function store(name) { var items = [].slice.call(arguments, 1); //[\'money\'] in first case items.forEach(function (item) { vault.customer[name].push(item); }); } and that will be equivalent to the following code in

Creating range in JavaScript - strange syntax

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:02:20
I've run into the following code in the es-discuss mailing list: Array.apply(null, { length: 5 }).map(Number.call, Number); This produces [0, 1, 2, 3, 4] Why is this the result of the code? What's happening here? Zirak Understanding this "hack" requires understanding several things: Why we don't just do Array(5).map(...) How Function.prototype.apply handles arguments How Array handles multiple arguments How the Number function handles arguments What Function.prototype.call does They're rather advanced topics in javascript, so this will be more-than-rather long. We'll start from the top. Buckle

Getting a reference to the global object in an unknown environment in strict mode

不羁岁月 提交于 2019-11-26 09:03:01
问题 What is the recommended way to get a handle to the global object in ES5 strict mode in an unknown host environment ? ECMAScript doesn\'t provide a built-in way to reference the global object that I\'m aware of. If it does, this is the answer I\'m looking for. In a known environment , the global object usually has a self-referential property. Since the global object is the VO for the global scope, properties of the global object are global variables, so we can use them get a handle to the

Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?

旧街凉风 提交于 2019-11-26 08:57:05
I'm working on a JavaScript project, and was just wondering why an object instance doesn't inherit the defineProperty() and other methods, rather than having to call the superclass (superobject?) Object method. I've looked at the MDN docs , and there are in fact "non-standard" property methods. But those are deprecated. Why would the move be to the Object methods? It seems to me that something like instance.defineProperty(...) is better than Object.defineProperty(instance, ...) . I would say the same about some of the other Object methods as well. It's to avoid collisions - in general, issues

Object.defineProperty in ES5?

二次信任 提交于 2019-11-26 08:30:25
问题 I\'m seeing posts about a \'new\' Object.create that makes enumeration configurable. However, it relies on a Object.defineProperty method. I can\'t find a cross browser implementation for this method. Are we stuck writing for the old Object.create? I can\'t write things that won\'t work in IE6/7. 回答1: There are several things that you can't emulate from the ECMAScript 5 Object.create method on an ECMAScript 3 environment. As you saw, the properties argument will give you problems since in E3

What object javascript function is bound to (what is its “this”)?

别等时光非礼了梦想. 提交于 2019-11-26 07:47:51
问题 I know that inside the function it is this . var func = function { return this.f === arguments.callee; // => true, if bound to some object // => false, if is bound to null, because this.f === undefined } var f = func; // not bound to anything; var obj = {}; obj1.f = func; // bound to obj1 if called as obj1.f(), but not bound if called as func() var bound = f.bind(obj2) // bound to obj2 if called as obj2.f() or as bound() Edited: You can\'t actually call obj2.f() as f doesn\'t become a

React: Which is recommended arrow or normal function?

拥有回忆 提交于 2019-11-26 04:52:02
问题 I started using arrow functions after I felt doing manual function/object bindings and scope related issues are headache but very rencently I came to know that it’s better to use normal function(ES5) than arrow function(ES6). My understanding about these functions Normal function in React: Bind object/function manually in order to play with state or props inside the function and to avoid scope related issues Bind object/function always in constructor but not directly in render If you do it in