es6-class

Node.js ES6 Class unable to call class method from within class method when using Express.js

蹲街弑〆低调 提交于 2019-12-02 12:36:56
问题 Nb. This is driving me a little nuts and I've been around the houses a few times. However I'm fairly new to ES6 and JS as a whole and fully understand that a JS Class is not like that of Classes found in other languages and might be approaching this completely wrong. I'm running the following code which is using Express.js (v4.16.3) and body-parser (v1.18.2) on Node v8.9.0. app.post('/api/v1/user/update', urlencodedParser, user.update); The code calls 'urlencodedParser' which is acting as

Node.js ES6 Class unable to call class method from within class method when using Express.js

亡梦爱人 提交于 2019-12-02 07:17:35
Nb. This is driving me a little nuts and I've been around the houses a few times. However I'm fairly new to ES6 and JS as a whole and fully understand that a JS Class is not like that of Classes found in other languages and might be approaching this completely wrong. I'm running the following code which is using Express.js (v4.16.3) and body-parser (v1.18.2) on Node v8.9.0. app.post('/api/v1/user/update', urlencodedParser, user.update); The code calls 'urlencodedParser' which is acting as middleware to provide 'req' with 'req.body' so that I can pull out the form fields. 'user' is a class

ES6 javascript class inheritance, why we need call to super() from derived class

筅森魡賤 提交于 2019-12-02 06:45:52
问题 In javascript ES6, in inheritance, if derived class has constructor, why it is mandatory to call super from derived constructor ? few failed examples are -: . Base with constructor but derived not calling super- class Base{constructor(){}} class Derived{constructor(){}} var d = new Derived(); // fails - ReferenceError: this is not defined 回答1: ...it seems it is mandatory to have constructor function in base class. Not really. If you don't provide one, one will be provided for you by the

Difference between constructor in ES6 class and constructor in prototype?

懵懂的女人 提交于 2019-12-02 06:25:47
Both ES6 class and prototype of function have a contructor , but I'm wondering are they the same? Let me give more explanations. So, I create a Cat function, for instance: const Cat = function (name) { this.name = name; }; The Cat has the following prototype: This constructor can be lost if I type smth. like Cat.prototype = {}; , but new Cat('Name'); will continue working. Ang we have the following syntax in ES6: class Dog { constructor(name) { this.name = name; } } The class also has constructor and it looks just like a simple function. Since classes are just a syntactic sygar over prototype

Using eval method to get class from string in Firefox

六眼飞鱼酱① 提交于 2019-12-02 05:29:37
问题 What I tried (which works in chrome) var class_str = "class Test {};"; var a = eval(class_str); console.log(new a()); Raises following error in Firefox 46: TypeError: a is not a constructor a is undefined and using new A() returns ReferenceError: A is not defined . What is different on Firefox? 回答1: Putting the whole class string in parentheses works. Fixed code: var class_str = "(class Test {})"; var a = eval(class_str); console.log(new a()); 回答2: I tried another method that works just as

Using eval method to get class from string in Firefox

你。 提交于 2019-12-02 02:13:38
What I tried (which works in chrome) var class_str = "class Test {};"; var a = eval(class_str); console.log(new a()); Raises following error in Firefox 46: TypeError: a is not a constructor a is undefined and using new A() returns ReferenceError: A is not defined . What is different on Firefox? Putting the whole class string in parentheses works. Fixed code: var class_str = "(class Test {})"; var a = eval(class_str); console.log(new a()); I tried another method that works just as using parentheses and parentheses seems much simpler as it doesn't pollute global names. result = eval(`(class a{})

How to redefine JavaScript (NOT CSS) classes, in the console?

為{幸葍}努か 提交于 2019-12-01 08:42:29
问题 I'm fairly new to JS classes, and am doing mostly back-end work. I was playing around with the new JS classes and so I started going through the examples here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes I went to the chrome (chromium) developer tools console and I wrote the Polygon class: class Polygon { constructor(height, width) { this.height = height; this.width = width; } } Then I wanted to redefine the class, according to the example containing the methods,

Serializing an ES6 class object as JSON

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 02:55:43
class MyClass { constructor() { this.foo = 3 } } var myClass = new MyClass() I'd like to serialize myClass object to json. One easy way I can think of is, since every member is actually javascript object (array, etc..) I guess I can maintain a variable to hold the member variables. this.prop.foo = this.foo and so on. I expected to find a toJSON/fromJSON library for class objects since I used them with other languages such as swift/java, but couldn't find one for javascript. Maybe class construct is too new, or what I'm asking can be somehow easily achieved without a library. As with any other

TypeScript Unexpected token, A constructor, method, accessor or property was expected

好久不见. 提交于 2019-11-30 19:05:18
Just trying to write a function within a class using typescript. class Test { function add(x: number, y: number): number { return x + y; } } This results in the following error: TypeScript Unexpected token, A constructor, method, accessor or property was expected. I copied the example from: https://www.typescriptlang.org/docs/handbook/functions.html Am I missing something? I'm confused! You shouldn't use the function keyword in a Typescript class definition. Try this instead: class Test { add(x: number, y: number): number { return x + y; } } TypeScript does not allow function declarations as

Classes with static arrow functions

天涯浪子 提交于 2019-11-30 18:34:52
I'm currently implementing the static land specification (an alternative of fantasy land). I want to not only use plain objects as types but also ES2015 classes with static methods. I've implemented these static methods as arrow functions in curried form instead of normal functions. However, this isn't possible with ES2015 classes: class List extends Array { static map = f => xs => xs.map(x => f(x)) static of = x => [x] } My map doesn't need its own this , because it is merely a curried function on the List constructor. To make it work I have to write static map(f) { return xs => xs.map(x => f