es2015

How to write a Mongoose model in ES6 / ES2015

落花浮王杯 提交于 2020-12-29 08:57:48
问题 I want to write my mongoose model in ES6. Basically replace module.exports and other ES5 things wherever possible. Here is what I have. import mongoose from 'mongoose' class Blacklist extends mongoose.Schema { constructor() { super({ type: String, ip: String, details: String, reason: String }) } } export default mongoose.model('Blacklist', Blacklist) I see this error in the console. if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization; ^

ES6's export and curly braces

╄→гoц情女王★ 提交于 2020-08-22 05:35:09
问题 I saw a code got posted in a chat channel. At the very end of his code is export {UserInformation}; There were groups saying that the syntax is wrong. Some were saying it is fine as long as the variable exists. So which group is right? It's my first time seeing this kind of syntax too. I've never seen curly braces in export. I've only used them in import. Like this import {method} from 'someModule'; If I was writing it, I would write it as export default UserInformation; I don't want to

ES6's export and curly braces

℡╲_俬逩灬. 提交于 2020-08-22 05:34:05
问题 I saw a code got posted in a chat channel. At the very end of his code is export {UserInformation}; There were groups saying that the syntax is wrong. Some were saying it is fine as long as the variable exists. So which group is right? It's my first time seeing this kind of syntax too. I've never seen curly braces in export. I've only used them in import. Like this import {method} from 'someModule'; If I was writing it, I would write it as export default UserInformation; I don't want to

ES6 Generator 初体验

孤街浪徒 提交于 2020-03-21 04:17:01
3 月,跳不动了?>>> ES6 Generator 初体验 听说 ES6 的 Generator 是一个很神奇的函数,所以去了解了一下。 因为它不同于以往的寻常函数,但是带来的体验却非常好 。这里首先讲了 Generator 是什么,分割线后面用了一个例子来说明 Generator 到底好在哪里 ~ (可以选择性阅读~ ) Generator 是一种异步编程解决方案,不明白?往下看 ~ Generator 到底是什么?官方文档是这样说的:“Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.” 意思就是 Generator 函数内部的执行是可以暂停的。你能够执行到一半退出函数,同时执行到当前的上下文环境(包括变量等等)全部被保存下来,如果我们稍后又进来继续执行下一段,此时是在上次状态的基础上继续往下执行。感觉和别的函数很不一样,如何去理解呢? 直接上代码: var myGen = function*(){ // 定义一个 Generator 函数 var one = yield 1; var two = yield 2; var three = yield 3;

Grunt config with es6

Deadly 提交于 2019-12-23 08:57:18
问题 It is possible to write grunt config files in es6 like this? //Gruntfile.js module.exports = function (grunt) { var arr = [1,2,3]; arr.forEach(val => { ... }); ... } 回答1: One possible way to do this painlessly is to use Babel's babel-register module like this: Installation: npm install babel-register --save-dev .babelrc: { presets: ["es2015"] } Gruntfile.js: require('babel-register') module.exports = require('./Gruntfile.es').default Gruntfile.es export default function(grunt) { grunt

ReactJS setState only works when nested inside setState

放肆的年华 提交于 2019-12-22 08:06:05
问题 The issue: When I use this.setState and I output the state in the callback, it doesn't change at all but when I nest the setstate inside a setstate it will then work correctly. Example: This doesn't work - this.setState({ data: newData }); This does work - this.setState({ data: newData }, () => { this.setState({ data: newData }); }); Does this have something to do with the way react batches state updates? This is the actual code in which the setstate doesn't work unless I nest it (I've tried

Is React.Component the default extension when exporting?

百般思念 提交于 2019-12-19 10:36:12
问题 I am looking through some React projects, and sometimes see- export default () => { But other times I see- export default class Entry extends React.Component { . Is there any difference between the two, viz. does export automatically extend React.Component ? What is the best practice? 回答1: The export default () => you see is a React 0.14+ "Functional Component". It's a new more concise syntax for writing React components. Both it and the other syntax are fine. These components behave just

How to properly bind current object context in ES6 using babelify

喜夏-厌秋 提交于 2019-12-13 00:29:30
问题 I'm trying to bind current instance to the class method, please note ES6 syntax. class SomeClass { search() => { ... } } Which is 100% legit code, however, babelify doesn't want to compile it SyntaxError: /Users/vladmiller/Projects/test/test/client/test/app/pages/Search.react.js: Unexpected token (50:26) while parsing file: /Users/vladmiller/Projects/test/test/client/test/app/pages/Search.react.js\ Instead, now I have to bind context in class constructor class SomeClass { constructor() { this

Failing to subclass builtin String object

喜你入骨 提交于 2019-12-07 07:13:09
问题 I've been experimenting with subclassing the built-in String object in ES2015 using Node 5.3.0. I'm running the code untranspiled using a bunch of harmony flags. Here's the full command: node --harmony --harmony_modules --harmony_destructuring --harmony_rest_parameters --harmony_arrow_functions --harmony_spreadcalls --harmony_object --harmony_default_parameters --harmony_new_target --harmony_reflect --harmony_modules ~/t.js Given that the spec specifically says the String object is made to be

快速了解ES6

[亡魂溺海] 提交于 2019-12-05 22:25:56
ECMAScript6(简称ES6),是JavaScript的下一代标准,因为ES6是在2015年发布的,所以又称ECMAScript2015(ES6 === ECMAScript2015) 目前不是所有的浏览器都完全兼容ES6,但很多程序猿已经开始使用ES6了,所以了解并逐步掌握ES6,甚至在你的项目中使用ES6,是非常有必要的,至少也要看得懂小伙伴写的ES6代码吧? 在说ES6之前,我们先来了解下Babel,这是个什么鬼?它是一个广泛使用的ES6转码器,可以将我们写的ES6代码转换成能在当前所有浏览器中执行的ES5代码。你可以在你习惯使用的工具中集成Babel,方便它来工作,具体使用请查看Babel官网( http://babeljs.io ) 下面我们来看下最常用的一些ES6特性: let,const class,extends,super arrow functions template string destructuring default rest arguments let,const let是用来声明变量的,与var相似,const是定义常量的。我们先来看下面的例子 while (true) { var name = 'obama'; console.log(name); //obama break; } console.log(name); //obama