ecmascript-5

Can I disable ECMAscript strict mode for specific functions?

给你一囗甜甜゛ 提交于 2019-12-17 08:34:32
问题 I don't find anything about my question here on MDC or the ECMAscript specifications. Probably somebody knows a more 'hacky' way to solve this. I'm calling "use strict" on every javascript file in my environment. All my files start like this (function(win, doc, undef) { "use strict"; // code & functions }(window, window.document)); Now, I have a custom function which handles errors. That functions uses the .caller property to provide a context stack trace . Looks like this: var chain =

How to explain object references in ECMAScript terms?

不羁的心 提交于 2019-12-17 07:40:46
问题 Consider this: var a = {}, b = a; In terms of the spec, b = a boils down to PutValue(b, GetValue(a)) , right? And GetValue(a) uses GetBindingValue("a", strictFlag) abstract operation, which returns "the value" in a . And "the value" is "the object" originally assigned to a . Then "the object" is stored in b , just like any other value would. But what is "the object" precisely? Where does the specification say that values of the Object type behave differently than primitives? Is it only that

Why do catch clauses have their own lexical environment?

不问归期 提交于 2019-12-17 06:51:35
问题 Consider the following excerpt from ECMA-262 v5.1 (which I recently saw in this question): 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 some specific syntactic structure of ECMAScript

Extending Object.prototype JavaScript

邮差的信 提交于 2019-12-17 04:31:10
问题 I am not asking if this is okay: Object.prototype.method = function(){}; This is deemed evil by pretty much everyone, considering it messes up for(var i in obj) . The Real Question Ignoring Incompetent browsers(browsers that don't support Object.defineProperty ) Potential for property collision or overriding Assuming you have some incredibly useful method, is this considered wrong/unethical? Object.defineProperty(Object.prototype, 'methodOnSteriods',{ value: function(){ /* Makes breakfast,

What is the difference between 'let' and 'const' ECMAScript 2015 (ES6)?

你离开我真会死。 提交于 2019-12-17 03:08:08
问题 I'm wondering what is the difference between let and const in ES6 . Both of them are block scoped, as the example in the following code: const PI = 3.14; console.log(PI); PI = 3; console.log(PI); const PI = 4; console.log(PI); var PI = 5; console.log(PI); In ES5 the output will be: 3.14 3.14 3.14 3.14 But in ES6 it will be: 3.14 3 4 5 I'm wondering why ES6 allows the change of const value, the question is why should we use 'const' now? we can use 'let' instead? Note : jsbin can be used for

Is it true that every function in JavaScript is a closure?

妖精的绣舞 提交于 2019-12-17 02:32:07
问题 I understand that every function in JavaScript is a first-class object and it has an internal property [[scope]] which hosts the binding records of the function's free variables. However, there are two special cases. Is the function created by Function constructor also a closure? The function object created by Function constructor is special, because its [[scope]] may not refer to the lexical environments of its outer functions, but only the global context. For example, var a = 1; var fn =

Dynamically set property of nested object

廉价感情. 提交于 2019-12-17 02:28:32
问题 I have an object that could be any number of levels deep and could have any existing properties. For example: var obj = { db: { mongodb: { host: 'localhost' } } }; On that I would like to set (or overwrite) properties like so: set('db.mongodb.user', 'root'); // or: set('foo.bar', 'baz'); Where the property string can have any depth, and the value can be any type/thing. Objects and arrays as values don't need to be merged, should the property key already exist. Previous example would produce

What does [].forEach.call() do in JavaScript?

时光毁灭记忆、已成空白 提交于 2019-12-16 21:02:08
问题 I was looking at some snippets of code, and I found multiple elements calling a function over a node list with a forEach applied to an empty array. For example I have something like: [].forEach.call( document.querySelectorAll('a'), function(el) { // whatever with the current node }); but I can't understand how it works. Can anyone explain me the behaviour of the empty array in front of the forEach and how the call works? 回答1: [] is an array. This array isn't used at all. It's being put on the

How to use es6 constructor instructions with a different context

三世轮回 提交于 2019-12-14 00:52:15
问题 Is it possible to use es6 constructor instructions on another instance by changing the "this" context (call, apply or other)? This is possible using es5 "classes". Here is a small example of what I mean: function ES5() { this.foo = 'foo'; } class ES6 { constructor() { this.bar = 'bar'; } } var a = new ES6(); ES5.call(a); console.log(a.foo + a.bar); //foobar var b = new ES5(); //Reflect.construct(ES6); ?? ES6.call(b); //TypeError: Class constructor ES6 cannot be invoked without 'new' console

Classes in JavaScript ECMAScript 5?

安稳与你 提交于 2019-12-13 18:11:24
问题 I would like to get to know if there are classes in JavaScript ECMAScript 5? I read some literature and there are different opinions about it. I know that it is possible to emulate classes, but I am not sure if they are called classes. Do you maybe also have some proofs? Are these classes simply pseudo classes or what is the correct term for it? 回答1: JavaScript/ECMAScript is a prototype-based programming language. Wikipedia’s definition of this is: Prototype-based programming is a style of