ecmascript-5

&& evaluation issue

ぐ巨炮叔叔 提交于 2019-12-10 13:25:35
问题 As far as I know, The logical && works like the following var test = false; var foo = test && 42; This code, will assign 42 to foo only if the first condition gets evaluated to true . So in this example, foo will keep its current value. I'm wondering why this snippet doesn't work at all: var test = ""; var foo = test && 42; Now, foo gets the value from test assigned. I'm very confused. The empty string is one of Javascripts falsy values , so why would the && operator fail in this scenario ?

Writing ECMAScript5 compliant code

北城余情 提交于 2019-12-10 11:13:09
问题 I want to build a library in JavaScript/JScript/ECMAScript...whatever you want to call it, which will target modern standards (HTML5, CSS3, ESv5) with that in mind, any browser that supports the standard! Now I know there are already plenty of useful libraries out there, i.e. jQuery and MooTools. Of course they're great and I already use those where necessary, but I should not be forced to jump on the same bandwagon as every other developer just because it's popular! So for the sake of the

Jint Array functions ECMA 5.1

丶灬走出姿态 提交于 2019-12-10 10:53:36
问题 I'm attempting to use Jint (v2.10.4.0) to translate one arbitrary JSON structure to another. However, I am having issues with using map . According to the ECMA 5.1 language spec, map should exist on Array.prototye. However, when I attempt to use it, I get an error: Jint.Runtime.JavaScriptException: 'Object has no method 'map'' I'm testing this like Engine engine = new Engine(); var doubles = engine.SetValue("x", "[ 1, 2, 3, 4, 5 ]") .Execute("x.map(function(a){ return a + a; })")

JavaScript: Testing variables for undefined value; testing whether object is array

怎甘沉沦 提交于 2019-12-10 08:45:55
问题 Is there a difference between typeof value === "undefined" and value === undefined ? Why did JavaScript need Array.isArray() in ECMAScript 5? Can't I just call value instanceof Array to determine whether an given variable is an array? 回答1: //var value; There is no var declaration. The variable was never declared // check againts undeclared variables typeof value === "undefined"; // works // check againts declared variables with no value value === undefined; // ReferenceError: value is not

ES6 as the typescript target compiler option for angularjs or angular2

若如初见. 提交于 2019-12-10 08:17:12
问题 The compiler option for my angularjs application is as below. Should I use any other package to transpile es6 to es5 again if I change the target to es6 ? { "compilerOptions": { "target": "es5", // Change this to es6 "module": "commonjs", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false, "outDir": "./wwwroot/app/" }, "exclude": [ "node_modules", "wwwroot" ] } 回答1: Targeting ES5 is currently a baseline requirement

How is strict mode (“use strict”;) inherited by functions?

丶灬走出姿态 提交于 2019-12-10 04:23:05
问题 Here is my code that seems to indicate that the answer is yes - http://jsfiddle.net/4nKqu/ var Foo = function() { 'use strict' return { foo: function() { a = 10 alert('a = ' + a) } } }() try { Foo.foo() } catch (e) { alert(e) } Could you please cite the statements from the standard that clarifies that 'use strict' is automatically applied to all closures and functions defined within a function to which we have applied 'use strict' ? 回答1: The relevant part of the spec: http://www.ecma

Javascript get and set availability in browsers

旧街凉风 提交于 2019-12-10 03:31:57
问题 Which browsers do not support the get and set methods for object prototypes? I believe this is a feature of ES5, an I know it works in Chrome, but I am wondering if it is safe to use for ajax apps. Here's an example: var foo = function () {}; foo.prototype = { get name () { return this._name; }, set name (n) { this._name = n || "bar"; } }; 回答1: Here's a compatibility table for you. http://kangax.github.com/es5-compat-table/ See the Getter in property initializer and Setter in property

'var' and 'let' in Typescript 1.5

耗尽温柔 提交于 2019-12-10 01:21:51
问题 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

How does JavaScript “writable” property descriptor work?

假如想象 提交于 2019-12-09 19:10:29
问题 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? 回答1:

JavaScript: Can ECMAScript 5's Strict Mode (“use strict”) be enabled using single quotes ('use strict')?

落爺英雄遲暮 提交于 2019-12-09 14:00:04
问题 JavaScript doesn't care if your Strings are double-quoted "double" or single-quoted 'single' . Every example of ECMAScript 5's strict mode has it enabled by "use strict" in double-quotes. Can I do the following (single-quotes): alert(function(){ 'use strict'; return !this; }()); This will return true if Strict mode is enabled, and false if it is not. 回答1: For you, without using a browser that supports strict mode: A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose