ecmascript-5

Array.prototype.forEach alternative implementation parameters

主宰稳场 提交于 2019-12-05 00:55:39
问题 When working on my latest web application and needing to use the Array.forEach function, I constantly found the following code used to add support to older browsers that do not have the function built in. /** * Copyright (c) Mozilla Foundation http://www.mozilla.org/ * This code is available under the terms of the MIT License */ if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length >>> 0; if (typeof fun != "function") { throw new TypeError

How to detect ECMAscript version?

谁说我不能喝 提交于 2019-12-05 00:50:07
I want to see what ECMAscript version I'm using in my browser(e.g,chrome 59) ,because there is some difference between ECMAscript3 and ECMAscript5 when dealing with something RegExp stuff. I've found the relevant information about this,but I can't find a specific answer about how to detect the ECMAscript version. Thank's in advanced. May be you can try to use some data structures that are specifically added in ES6 like Map , Set etc. This is to differentiate between ES5 and ES6 but you can look up for features that are added in ES5 which are not present in ES3 in your case? try { var k = new

Are there semicolon insertion dangers with continuing operators on next line?

你说的曾经没有我的故事 提交于 2019-12-04 23:43:46
Historically, I like to break expressions so that the "it's clearly incomplete" bias is shown on the continued line: var something = foo + bar + baz(mumble); This is an attitude that comes from working in languages which need semicolons to terminate expressions. The first line is already obviously incomplete due to no-semicolon, so it's better to make it clear to the reader that the second line is not complete. The alternative would be: var something = foo + bar + baz(mumble); That's not as good for me. Now the only way to tell that baz(mumble); isn't standalone (indentation aside) is to scan

'var' and 'let' in Typescript 1.5

China☆狼群 提交于 2019-12-04 22:45:23
This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . What exactly is the difference between using either ' var ' or ' let ' in Typescript? I know that 'let' allows the variable to be defined further into a scope without it being used outside of said scope. That is obviously a nice advantage for iterators in for loops. I know this is an ES6 definition, so compiling to ES6 should look nearly identical in terms of 'var' and 'let'. But, when it compiles down into ES5 javascript which doesn't support 'let', what

Advantage of using Object.create

こ雲淡風輕ζ 提交于 2019-12-04 18:34:16
问题 Similar to, but different from this question. The code below is from JavaScript: The Definitive Guide. He's basically defining an inherit method that defers to Object.create if it exists, otherwise doing plain old Javascript inheritance using constructors and swapping prototypes around. My question is, since Object.create doesn't exist on plenty of common browsers IE, what's the point of even trying to use it? It certainly clutters up the code, and one of the commenters on the previous

Why the property “prototype” is absent in definition methods of ES6 classes

余生颓废 提交于 2019-12-04 18:02:24
Es6, Classes there. We have the method (go) like this : the example in ES6 class X{ go(){} } var y = new X(); var z = new y.go(); console.log(z) Example of the Error Screen Shot: We don't have property prototype of this method (go), so we can't create new Object from this method. This is the fact. But I can't understand WHY? Why the developers of javascript in ES6 don't let me use this functionality. Vice versa in ES5 we can create new instance from Object's methods. Of course, it works from prototype's methods too the example in Es5 function X (){} X.prototype.go = function(){} var y = new X(

Why are JavaScript Arguments objects mutated by assignment to parameter?

亡梦爱人 提交于 2019-12-04 17:51:18
问题 What is the rationale behind this behaviour? function f(x) { console.log(arguments[0]); x = 42; console.log(arguments[0]); } f(1); // => 1 // => 42 Perhaps this was a genuine mistake. Which section of the ECMAScript specification defines this behaviour? 回答1: Actually, in strict mode, this does not happen as you can see here. If you read section 10.6 of the ECMA Standard, in particular Note 1, you'll see: For non-strict mode functions the array index (defined in 15.4) named data properties of

JavaScript Closures Concerning Unreferenced Variables

依然范特西╮ 提交于 2019-12-04 13:04:25
问题 I'm aware of the great posts on Closures here and here, but neither seems to address the particular case I have in mind. The question is best demonstrated with code: function foo() { var x = {}; var y = "whatever"; return function bar() { alert(y); }; } var z = foo(); Referencing y within bar invokes a closure, and so long as I keep z around the garbage collector won't clean up y . The question is -- what happens to x ? Is it held by that closure too even though it doesn't get referenced?

How does JavaScript “writable” property descriptor work?

和自甴很熟 提交于 2019-12-04 12:36:01
Why does the JavaScript "writable" property descriptor not forbid any property changes? For example: var TheDarkKnight = Object.create(Superhero, { "name": { value:"Batman", writable:"false" } }); TheDarkKnight.name; //"Batman"; TheDarkKnight.name = "Superman"; TheDarkKnight.name; //"Superman"; I thought TheDarkKnight.name should still return "Batman" after I tried to change it to another value because I set the "writable" property descriptor to false . So how to use it in the right way? It should be false , not "false" . In other words, it should be a boolean. If you don't pass a boolean,

Object.constructor===Object.constructor.constructor // why?

五迷三道 提交于 2019-12-04 12:33:04
问题 stated here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function the constructor property of an instance of a function object "specifies the function that creates an object's prototype". This is confusing, so Object.constructor is "the function that creates an object's prototype"? What object is "an object" exactly? I'm trying to understand why is Object.constructor's constructor property itself? as such: Object.constructor===Object.constructor.constructor // why?