es6-class

React defining state without a constructor [duplicate]

醉酒当歌 提交于 2019-12-08 19:20:54
问题 This question already has answers here : init state without constructor in react (3 answers) Closed 2 years ago . I've seen some react developers define state without a constructor. I like the fact that is simplifies the code, but is it safe to do so? class Dog extends React.Component { state = { sound: 'Woof' } return ( <p>Dog says { this.state.sound }</p> ) } I apologize in advance for the over simplified example. 回答1: Its exactly the same thing. Take a look at the javascript that babel

Merging 2 classes

ぃ、小莉子 提交于 2019-12-08 14:46:34
I want to merge 2 classes or add all methods from 1 class to another. In the future It will be the more classes than just a (ContainerClient) class. Client and ContainerClient I tried this but I think It can be done even easier. for (var a of Object.getOwnPropertyNames(ContainerClient.prototype)) { Client.prototype[a] = ContainerClient.prototype[a]; } Principle: ContainerClient depends on Client Data.js class Data { constructor () { } } Client.js class Client extends Data { me () { return { id: '1', }; } } Container.js class Container extends Data { containers () { return [ { clientId: '1', id

Error logs are not passed to the error log file when debug logs does. Why does that happen?

烈酒焚心 提交于 2019-12-08 05:34:20
问题 I am trying to create a simple logger class with Winston. I created two loggers named Logger and Error which extends from Logger . The Logger class by default creates all the logs in al-logs.log and Error class when extending from Logger adds an additional transport to create all error logs inside error-logs.log . But no logs are created inside error-logs.log . What am I missing? Here is what I did: First, I created the Logger class: class Logger { constructor(serviceName, label = "") { this

How to import and export javascript ES6 classes

守給你的承諾、 提交于 2019-12-07 13:37:06
问题 I am new to javascript and nodejs and I'm using this project to develop my skill and learn the new technologies. Currently my project uses multiple classes that depend on one another. The class files are in different directories and I am currently trying to use export and require statements to allow classes to be referenced in other files. I am also using browserify and watchify to bundle all the files together, to simplify the html script tag. Here's Project Layout (JavaScript Folder) -Base

Difference between Static function declaration and the normal function declaration in Javascript?

懵懂的女人 提交于 2019-12-07 04:49:45
问题 There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below. class className { static fucntionName() { } } another way of is declaring is through the tradition javascript style as showed below. export function functionName() { } I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in

Cannot call a method within a class it defined it in ES6 in Node.js [duplicate]

我与影子孤独终老i 提交于 2019-12-06 20:04:25
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . I am making an app using Node.js, Express.js and MongoDB. I am using a MVC pattern and also have separate file for routes. I am trying me make a Controller class, in which a method calls another method declared within it. But I cannot seem to be able to do this. I get "Cannot read property '' of undefined". index.js file let express = require('express'); let app =

Error logs are not passed to the error log file when debug logs does. Why does that happen?

五迷三道 提交于 2019-12-06 16:34:45
I am trying to create a simple logger class with Winston . I created two loggers named Logger and Error which extends from Logger . The Logger class by default creates all the logs in al-logs.log and Error class when extending from Logger adds an additional transport to create all error logs inside error-logs.log . But no logs are created inside error-logs.log . What am I missing? Here is what I did: First, I created the Logger class: class Logger { constructor(serviceName, label = "") { this.logger = winston.createLogger({ level: "debug", format: winston.format.combine( winston.format.label({

Set eslint rule for unused class methods in React component

Deadly 提交于 2019-12-05 15:40:13
I am trying to set an eslint rule for methods in class that are never used. Like in the following react component I have a method unUsedMethod which is never used, but eslint does not show an error for it. class Sample extends Component { unUsedMethod() { console.log('I am never used'); } render() { return 'Hello!'; } } My eslint file looks like this { "parser": "babel-eslint", "env": { "browser": true, "es6": true, "node": true }, "extends": ["eslint:recommended", "plugin:react/recommended"], "parserOptions": { "ecmaFeatures": { "experimentalObjectRestSpread": true, "jsx": true, "sourceType":

Difference between Static function declaration and the normal function declaration in Javascript?

柔情痞子 提交于 2019-12-05 10:41:26
There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below. class className { static fucntionName() { } } another way of is declaring is through the tradition javascript style as showed below. export function functionName() { } I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in javascript there is no need to instantiate the class in order to access the static function). Why not just use

Cannot call a method within a class it defined it in ES6 in Node.js [duplicate]

家住魔仙堡 提交于 2019-12-05 01:27:21
This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . I am making an app using Node.js, Express.js and MongoDB. I am using a MVC pattern and also have separate file for routes. I am trying me make a Controller class, in which a method calls another method declared within it. But I cannot seem to be able to do this. I get "Cannot read property '' of undefined". index.js file let express = require('express'); let app = express(); let productController = require('../controllers/ProductController'); app.post('/product',