ecmascript-5

Why is there no forEach method on Object in ECMAScript 5?

妖精的绣舞 提交于 2019-12-03 03:31:12
问题 ECMAScript 5's array.forEach(callback[, thisArg]) is very convenient to iterate on an array and has many advantage over the syntax with a for: It's more concise. It doesn't create variables that we need only for the purpose of iterating. It's creates a visibility scope for the local variables of the loop. It boosts the performance. Is there a reason why there is no object.forEach to replace for(var key in object) ? Of course, we could use a JavaScript implementation, like _.each or $.each but

Is there an i18n (Intl) shim for JavaScript?

十年热恋 提交于 2019-12-03 01:42:31
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.) Andy E 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 DateTimeFormat , but no support for Collator . Currently, for client-side browser environments, you

Scan Javascript for browser compatibility

风流意气都作罢 提交于 2019-12-02 23:24:06
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 that would fall into this category. You can use eslint-plugin-compat , a plugin for the ESlint linting

How to provide ECMAScript 5 (ES 5)-shim?

北战南征 提交于 2019-12-02 20:06:25
ECMAScript Fifth Edition (released December 2009) introduces a bunch of new methods (see this table for details). However, there still are older browsers out there which do not implement those new methods. Luckily, there exists a convenient script (written in JavaScript) - ES5-shim - which implements those methods manually in environments where they don't exist. However, I am not sure how to provide ES5-shim... Should I just "give" it to all browsers, like so: <script src="es5-shim.js"></scipt> Or should I include a check in order to only "bother" those browsers which really need it, like so:

Why do we need the isPrototypeOf at all?

醉酒当歌 提交于 2019-12-02 18:23:07
this page states: Note: isPrototypeOf differs from instanceof operator. In the expression object instanceof AFunction, the object prototype chain is checked against AFunction.prototype, not against AFunction itself Ok I don't really get what they are trying to tell us. Isn't object instanceof AFunction exactly the same as `AFunction.prototype.isPrototypeOf(object)? or am I wrong? Why do we need the isPrototypeOf at all? If i ever need to do p.isPrototypeOf(o) couldn't I just do o instanceof p.constructor ? Addtionally, is p.isPrototypeOf(o) functionally equivalent to p===Object.getPrototypeOf

type=“text/ecmascript” vs type=“text/javascript”

匆匆过客 提交于 2019-12-02 18:02:16
I was reading a book about learning JavaScript, and there was these paragraphs: ...in middle of 1997, Microsoft and Netscape, with associate of European Computer Manufactures Association, released the first version of a standard that named ECMAScript or with official form ECMA-262 ... As much as I was found in this book and something like this, JavaScript and ECMAScript are the same and are different just in name. From other hand, in Dreamweaver, bracket, and some other editors, there's some autocomplete suggestion like this: when I want to add a script tag to my page. I want to know if there

ECMAScript Regex for a multilined string

别说谁变了你拦得住时间么 提交于 2019-12-02 17:59:50
问题 I am writing the loading procedure for my application and it involves reading data from a file and creating an appropriate object with appropriate properties. The file consists of sequential entries (separated by a newline) in the following format: === OBJECT TYPE === <Property 1>: Value1 <Property 2>: Value2 === END OBJECT TYPE === Where the values are often strings which may consist of arbitrary characters, new-lines, etc. I want to create a std::regex which can match this format and allow

Shim vs. Sham: What is the difference?

无人久伴 提交于 2019-12-02 17:51:17
What is the difference between a shim an a sham? Is it enough to include es5-shim.min.js and es6-shim.min.js or should I also include es5-sham.min.js and es6-sham.min.js? According to this Github page the shims include all monkey-patches that faithfully represent the ES5 features. In other words: you can use the features provided by these files as if you were using ES5 proper. The shams, however contain those features that can not be emulated with other code. They basically provide the API, so your code doesn't crash but they don't provide the actual functionality. Which ones do you need? That

Javascript Reduce an empty array

假装没事ソ 提交于 2019-12-02 17:22:05
When I reduce the array, I am trying to get the number zero, but I dont clearly understand the behaviour of the function [].reduce(function(previousValue, currentValue){ return Number(previousValue) + Number(currentValue); }); result TypeError: Reduce of empty array with no initial value seems that if the array is empty I can't reduce it [""].reduce(function(previousValue, currentValue){ return Number(previousValue) + Number(currentValue); }); result "" If the only element in the array is an empty string, retrieves an empty string xdazz The second parameter is for initial value. [].reduce

ES5 Object.assign equivalent

我怕爱的太早我们不能终老 提交于 2019-12-02 17:09:27
I wanted to do something which is very straight-forward using Object.assign . var firstObj = {name : "Saba H.", rollNo : 1}; var secondObj = {college : "WCE"}; var wholeObj = Object.assign(firstObj, secondObj); console.log(wholeObj); // {name : "Saba H.", rollNo : 1, college : "WCE"} As Object.assign is part of ECMAScript6 harmony proposal and not supported across many browsers, is it possible to do with ES5? If not then is there any micro library? In underscore.js you can use like, _.extend(firstObj, secondObj); In jQuery, you can use, $.extend({},firstObj,secondObj); In pure javascipt, you