ecmascript-5

Shorthand for Object.create() with multiple properties

核能气质少年 提交于 2019-12-18 09:42:19
问题 If I want to create an object in JavaScript that has a prototype link to another object, but has several of it's own properties how can I do this? var object1 = { a: 1, b: 2 }; var object2 = Object.create( object1 ); object2.c = 3; object2.d = 4; console.log( object2 ); // my new object with object1 as it's prototype link My challenge here is that I have to set object2 's properties one at a time. My other option is: var object1 = { a: 1, b: 2 }; var object2 = { c: 3, d: 4 }; Object

Equivalent of set in ES6 to ES5

北城以北 提交于 2019-12-18 09:13:13
问题 I have a set over which I am iterating in ES6. I am trying to convert it to its equivalent in ES5. My build is getting failed because of ES6. That's why I am converting it to ES5. Here's my code in ES6 service.getDevices = function (date) { var result = []; var deviceList = devices[date.getTime()]; for (let item of deviceList) { // browser compatibility: support for ECMA6 result.push({"deviceName": item}); } return result; } I am getting error because of 'let'. I tried using for (var item in

Object.defineProperty polyfill

半世苍凉 提交于 2019-12-18 03:44:16
问题 I am currently writing a JavaScript API which is based on new features in ES5. It uses Object.defineProperty quite extensively. I have wrapped this into two new functions, called Object.createGetSetProperty and Object.createValueProperty I am however experiencing problems running this in older browsers (such as the dreaded, IE8) Consider the following code: Object.createGetSetProperty = function (object, property, get, set, enumerable, configurable) { if (!Object.defineProperty) throw new

Clarity on the difference between “LexicalEnvironment” and “VariableEnvironment” in ECMAScript/JavaScript

只谈情不闲聊 提交于 2019-12-18 03:43:39
问题 Could someone clarify what the difference is between these two, as they exist in the Execution context? It's hard for me to read the ECMA 262 v 5 specification and clearly see the difference. Thank You, 回答1: Both are components (of the same type) of Execution Contexts, but they serve distinct purposes (from the spec): LexicalEnvironment Identifies the Lexical Environment used to resolve identifier references made by code within this execution context. VariableEnvironment Identifies the

Clarity on the difference between “LexicalEnvironment” and “VariableEnvironment” in ECMAScript/JavaScript

♀尐吖头ヾ 提交于 2019-12-18 03:43:10
问题 Could someone clarify what the difference is between these two, as they exist in the Execution context? It's hard for me to read the ECMA 262 v 5 specification and clearly see the difference. Thank You, 回答1: Both are components (of the same type) of Execution Contexts, but they serve distinct purposes (from the spec): LexicalEnvironment Identifies the Lexical Environment used to resolve identifier references made by code within this execution context. VariableEnvironment Identifies the

Is it possible to emulate non-enumerable properties?

我与影子孤独终老i 提交于 2019-12-17 22:22:31
问题 ES5 has a enumerable flag. Example Example var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor , pd = getOwnPropertyDescriptor(Object.prototype, "toString"); assert(pd.enumerable === false, "enumerability has the wrong value"); Partial implementation Partial implementation is do-able by having Object.keys and Object.getOwnPropertyNames filter out new non-enumerable properties using the shimmed Object.defineProperty . Introduction This allows for properties to be non enumerable.

Is there a way in Eclipse to change the ECMAScript compliance level?

我只是一个虾纸丫 提交于 2019-12-17 18:52:12
问题 In Eclipse 4.5 (Mars) with installed JavaScript Development Tools (JSDT), the default compliance level for ECMAScript is set to ECMAScript 3 , which is already very outdated. And the related drop down list to change the compliance level is deactivated, so it's not possible to change the option. Is there a way to change those settings to a newer standard like ECMAScript 5 or ECMAScript 2015 ? 回答1: There is no way to use a newer compliance level directly in JSDT, but with a plugin called tern

Indirect eval call in strict mode

北城余情 提交于 2019-12-17 17:47:28
问题 I understand about how eval() works in non-strict contexts, however the case of using eval() in strict mode has completely befuddled me. When eval() is called directly in the global scope, variables are kept inside the new eval() scope: 'use strict'; eval('var a = 1;'); console.log(a); // ReferenceError: a is not defined However, if I perform an indirect call to eval() in the global scope (should be the same thing, right?), it acts as though it is not in strict mode (if you don't believe me,

Difference between freeze and seal

柔情痞子 提交于 2019-12-17 17:19:41
问题 I just heard about the JavaScript methods freeze and seal , which can be used to make any Object immutable. Here's a short example how to use it: var o1 = {}, o2 = {}; Object.freeze(o2); o1["a"] = "worked"; o2["a"] = "worked"; alert(o1["a"]); //prints "worked" alert(o2["a"]); //prints "undefined" What is the difference between freeze and seal ? Can they increase performance? 回答1: Object.seal It prevents adding and/or removing properties from the sealed object; using delete will return false

In ECMAScript5, what's the scope of “use strict”?

…衆ロ難τιáo~ 提交于 2019-12-17 15:46:52
问题 What scope does the strict mode pragma have in ECMAScript5? "use strict"; I'd like to do this (mainly because JSLint doesn't complain about it): "use strict"; (function () { // my stuff here... }()); But I'm not sure if that would break other code or not. I know that I can do this, which will scope the pragma to the function... (function () { "use strict"; // my stuff here... }()); but JSLint complains about it (when the "strict" JSLint option is enabled) because it thinks you're executing