ecmascript-5

Does JavaScript (ECMAScript5) Strict Mode offer significant performance advantages to merit widespread use?

十年热恋 提交于 2019-12-03 16:37:29
问题 I'm reading up a bit on using Strict Mode for JavaScript and it seems that, generally speaking, the idea is to force a more rigid set of rules onto the coder to ensure that the JS engine can optimise the code better. It almost feels like the JavaScript equivalent of "Option Explicit" in Visual Basic. If this is basically the net effect of applying Strict Mode to my code, would the performance difference be such that it would be worth applying out of habit rather than case-by-case? Are there

Array.prototype.forEach alternative implementation parameters

微笑、不失礼 提交于 2019-12-03 16:28:03
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(); } var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { fun.call(thisp, this[i

Accessors are only available when targeting ECMAScript 5 and higher

旧街凉风 提交于 2019-12-03 14:25:45
问题 I am trying to run this code but it is giving me following errors: Animal.ts(10,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. Animal.ts(14,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. interface IAnimal{ name : string; sayName():string; } class AnimalImpm implements IAnimal{ private _name : string = '[Animal]'; get name():string{ return this._name; } set name(name:string){ this._name = name; } constructor(name

Call ES5 class method from static method

孤街醉人 提交于 2019-12-03 13:55:18
问题 I want to call an inner function from a static function that was called without an instance, like so: Foo.Bar = function (options) { Autodesk.Viewing.Extension.call(this, options); ... this.innerFunc = function innerFunc(){ ... } }; Foo.Bar.prototype.constructor = Foo.Bar; Foo.Bar.SomeStaticFunc = function () { innerFunc(); } Use: Foo.Bar.SomeStaticFunc(); . But I get SomeStaticFunc is not a function . The example here uses a variable for the class, like var Foo.Bar = function (options) {...

Is there an i18n (Intl) shim for JavaScript?

别等时光非礼了梦想. 提交于 2019-12-03 12:06:20
问题 I am looking for a shim for the ECMAScript Internationalization API. Does anyone know of such a project? (Even if it's still currently a work-in-progress.) 回答1: Yes, there's a polyfill for ECMA-402 (aka ECMA Internationalization API Specification) available at https://github.com/andyearnshaw/Intl.js. For use in Node.js applications, you can install with NPM: npm install intl It's also available as a Bower component for the front-end: bower install intl There's support for NumberFormat and

How do I perform an export that is compatible with ES5 and ES6?

吃可爱长大的小学妹 提交于 2019-12-03 10:41:05
问题 I'm writing a "class" in node // mymodule/index.js function MyClass() {} MyClass.prototype.method1 = function() {..} usually I do module.exports = MyClass but I want my class available for both syntax var MyClass = require('mymodule') and import {MyClass} from 'mymodule' Which is the correct way to do it? 回答1: As far as writing an export that is compatible for both ES5 and ES6, Babel already takes care of that for you. (As communicated in the comments to your question. I'm only clarifying for

Why are JavaScript Arguments objects mutated by assignment to parameter?

对着背影说爱祢 提交于 2019-12-03 10:38:52
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? 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 an arguments object whose numeric name values are less than the number of formal parameters of the

Scan Javascript for browser compatibility

我的未来我决定 提交于 2019-12-03 09:11:10
问题 Is there a tool out there to scan my Javascript code for functions that may not be present in all browsers? My library is completely non-UI, so I don't care about how something is "displayed". What I'm looking for is something like in the Javascript MDN from Mozilla. For example, for Array.prototype.indexOf, they warn that it's a recent ECMAScript addition that is not present in all browsers (and typically provide a stub). What I'm looking for is a tool that'd list the functions in my code

Why does `“foo”.bar = 42;` throw `TypeError` in strict mode in ES6?

社会主义新天地 提交于 2019-12-03 08:21:58
问题 According to the ES5.1 spec, the program "use strict;" "foo".bar = 42; causes a String object to be created, assigns to a property on it, and then throws the object away, resulting in no observable effects - including any exceptions. (The absence of effect can be confirmed by trying it in an ES5-compatible JS implementation like that in Opera 12.) In modern JS implementations, it throws a TypeError instead—try it: "use strict"; "foo".bar = 42; I am pretty sure the new behaviour is mandated by

Button to show choose a file to upload dialog box

不问归期 提交于 2019-12-03 07:54:38
Instead of using an input type="file" html tag, is it possible to bring up a choose a file to upload dialog box by clicking a input type="button" ? Then when a file is selected from the choose a file to upload dialog box, the file path gets inserted into a regular html input type="text" tag? I've seem that gmail does something similar but not with buttons and text inputs, they simply have a link add file or something like that. When that link is clicked, it shows the select file(s) to upload by mail.google.com dialog box. When a file is clicked, the file name is shown on the screen. How are