ecmascript-5

Object.create instead of Constructors for inheritance

…衆ロ難τιáo~ 提交于 2020-01-14 02:47:10
问题 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'},

Does a function expression have its own scope/lexical environment

痴心易碎 提交于 2020-01-13 10:25:09
问题 I'm reading the Execution Context / Lexical Environment section of the ECMA 262 5 specification. It states the following: (emphasis added) A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with

How to initialize default data in ES5 Redux reducer?

女生的网名这么多〃 提交于 2020-01-10 19:43:32
问题 For the time being I can't use ES6/ES2015 and I'm stuck with ES5 for writing Redux reducers. Since the state parameter of a reducer has to be immutable, even when it's undefined, I came up with the following pattern: function myState( state, action ) { if ( typeof state === 'undefined' ) { return myState( { value1: 'foo', value2: 'bar' }, action ); } // actual state calculation here } Any alternative suggestions or comments on how to ensure a default value with ES5? Edit: After some questions

javascript hoisting var vs let [duplicate]

徘徊边缘 提交于 2020-01-07 03:07:09
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 4 years ago . I'm learning some ES6 features and of course came across the let keyword and its new scope (differs from var ) and I came across an example about the tricky scope of var and its hoisting. but I can't fully understand why I get this result: var events = ['click', 'dblclick', 'keydown', 'keyup']; for (var i = 0; i < events.length; i++) { var event = events[i];

Manipulating a complex object based on an input array

江枫思渺然 提交于 2020-01-05 07:09:33
问题 I have an complex object and based on an input array I need to modify properties inside of this complex object. Illustration is shown below. I need to group them based on field values and then add them to the "or" array of objects(if field value is same). If not same,directly add it to the "and" array of objects. Need to read each field from the input array,and check if this is present inside "filter" of "filterObj" and if present add it to "and" else add it to the "or" The format of both is

OLOO how to access private variable

强颜欢笑 提交于 2020-01-03 02:41:08
问题 In the following example I am using Kyle Simpson's "OLOO (Objects Linking to Other Objects) Pattern" to create a simple example of object serialization. I need to keep variable _data private (I am using a closure) and expose its properties only with getter and setters which are create on object instance level (in init ). Currently I defined function toJson inside init so it can access _data and it works but, I would like to know: If would be possible to move toJson function outside init

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

空扰寡人 提交于 2020-01-02 06:51:41
问题 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? 回答1: What you're

How to get name of JavaScript Class

吃可爱长大的小学妹 提交于 2020-01-02 06:41:43
问题 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

`this` in global scope in ECMAScript 6

倖福魔咒の 提交于 2020-01-02 04:28:10
问题 I've tried looking in the ES6 draft myself, but I'm not sure where to look: Can someone tell me if this in ES6 necessarily refers to the global object? Also, will this object have same members as the global scope? If you could answer for ES5 that would be helpful as well. I know this in global scope refers to the global object in the browser and in most other ES environments, like Node. I just want to know if that's the defined behavior by the spec or if that's extended behavior that

Factory function in a Typescript declares file, with and without the new keyword

余生颓废 提交于 2020-01-02 02:28:28
问题 The following code will create a factory function in ES5: function MyClass(val) { if (!(this instanceof MyClass)) { return new MyClass(val); } this.val = val; } This function can be called with or without the new keyword: var a = new MyClass(5); var b = MyClass(5); This works fine in Typescript, however I can't figure out how to create a declares file with merging that describes both behaviors. Is there a way to do this? 回答1: interface MyClass { val: {}; } interface MyClassConstructor { (val: