ecmascript-5

Is it possible to use ES5 JavaScript with Angular 2 instead of TypeScript?

梦想与她 提交于 2019-11-27 17:45:46
问题 Is it needed to learn TypeScript for Angular 2? Can Angular 2 be used with plain JavaScript ? Edit: I've seen that the languages used as ES6, ES7, Dart compile to JavaScript to be executed, but I haven't seen any reference to use ES5 JavaScript directly. 回答1: Yes, you can. Go read this guide. Pressing the ES5 tab on the code examples will show you regular ES5 JavaScript, as apposed to TypeScript. The API preview is, for obvious reasons, incomplete though. So you may not find the ES5 methods

null vs. undefined and their behaviour in JavaScript

我的梦境 提交于 2019-11-27 12:56:50
问题 So after a big argument/debate/discussion on the implementation of null and undefined in javascript I'd like somebody to explain the reasoning behind the implementation and why they differ in some circumstances. Some particular points I find troubling: null == undefined evaluates to true null + 1 equals 1 but undefined + 1 equal NaN if(!null) evaluates to true and if(null) evaluates to false but null == false evaluates to false. I've read the specification and I know how the results are

gulp babel, exports is not defined

孤街浪徒 提交于 2019-11-27 12:35:17
Consider the following example code (and maybe I am doing it wrong?) var FlareCurrency = { }; export {FlareCurrency}; I have the following task: gulp.task("compile:add-new-currency-minified", function(){ return gulp.src('src/add-new-currency/**/*.js') .pipe(babel()) .pipe(concat('Flare-AddNewCurrency.js')) .pipe(uglify({"preserveComments": "all"})) .pipe(gulp.dest('dist/minified/')); }); When I run this I get the following: "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var FlareCurrency={};exports.FlareCurrency=FlareCurrency; For the fun of it, I wanted to run it in the

How to implement private method in ES6 class with Traceur [duplicate]

醉酒当歌 提交于 2019-11-27 10:35:00
This question already has an answer here: Private properties in JavaScript ES6 classes 35 answers I use Traceur Compiler to have advantage with ES6 features now. I want to implement this stuff from ES5: function Animal() { var self = this, sayHi; sayHi = function() { self.hi(); }; this.hi = function() {/* ... */} } Currently traceur does not support private and public keywords ( from harmony ). And ES6 class syntax does not allow to use simple var (or let ) statements in class body. The only way that I am find is to simulate privates before class declaration. Something like: var sayHi =

Prototypical OO in JavaScript

…衆ロ難τιáo~ 提交于 2019-11-27 09:08:29
问题 TL;DR: Do we need factories/constructors in prototypical OO? Can we make a paradigm switch and drop them completely? The BackStory: I've been toying with doing prototypical OO in JavaScript lately and find that 99% of OO done in JavaScript is forcing classical OO patterns into it. My take on prototypical OO is that it involves two things. A static prototype of methods (and static data) and a data binding. We don't need factories or constructors. In JavaScript these are Object literals

How can I make old style classes work in typescript?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 08:09:57
问题 While converting a large number of files to typescript, I have many classes declared this way. function FooClass() { this.bar = 1; // error TS2683: 'this' implicitly has type 'any' // because it does not have a type annotation. } FooClass.prototype.myMethod = function() { // ... } How can I make this work with strict type checking turned on, while avoiding rewriting it all using class syntax? 回答1: The easiest way to get the above code to work is to add a this parameter to the function, like

Revisiting extending native prototypes after ECMAScript 5

荒凉一梦 提交于 2019-11-27 07:51:24
问题 Recently, given the changes to defining properties in ECMAScript 5, I have revisited the question of whether we can safely extend the native JavaScript prototypes. In truth, all along I have extended prototypes like Array and Function, but I avoided doing so with Object, for the obvious reasons. In unit testing with Jasmine, by adding Object.prototype specs to specs for my own personal framework, extending Object.prototype with non-enumerable functions has appeared to be safe. Data properties

Is the 'catch' method name of JS Promises/A+ invalid since it's a JS keyword?

可紊 提交于 2019-11-27 07:49:36
问题 I started to use JS Promises in a project recently. I noticed that every time I use .catch my JS linter complains. It does run and does what it should but I looked up the ECMAScript spec and it really looks like it is right: Since catch is a keyword it can not be used as an identifier. As I understand method names are identifiers, so this is invalid: Promise.reject("Duh").catch(alert); It should be this instead: Promise.reject("Duh")['catch'](alert); What am I missing? 回答1: What am I missing?

How can I define a default getter and setter using ECMAScript 5?

拟墨画扇 提交于 2019-11-27 07:42:08
问题 How can I specify a default getter for a prototype? With default getter I mean a function that is called if obj.undefinedProperty123 is called. I tried Object.prototype.get = function(property) {..} but this is not called in this case. 回答1: In ECMAScript 5, you can only intercept get/set operations on specific named properties (not universally all properties) via Object.defineProperty: Object.defineProperty(someObj, "someProp", { get: function() { console.log("you tried to get someObj

Can I disable ECMAscript strict mode for specific functions?

牧云@^-^@ 提交于 2019-11-27 06:56:52
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 = (function() { var _parent = _error, _ret = ''; while( _parent.caller ) { _ret += ' -> ' + _parent.caller.name