我正在尝试ES6,并希望在我的函数中包含一个属性,就像这样
var person = {
name: "jason",
shout: () => console.log("my name is ", this.name)
}
person.shout() // Should print out my name is jason
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码控制台时只记录日志my name is.我究竟做错了什么?
我想弄清楚为什么在对象文本的箭头函数被调用window为this.有人可以给我一些见解吗?
var arrowObject = {
name: 'arrowObject',
printName: () => {
console.log(this);
}
};
// Prints: Window {external: Object, chrome: Object ...}
arrowObject.printName();
Run Code Online (Sandbox Code Playgroud)
一个按预期工作的对象:
var functionObject = {
name: 'functionObject',
printName: function() {
console.log(this);
}
};
// Prints: Object {name: "functionObject"}
functionObject.printName();
Run Code Online (Sandbox Code Playgroud)
根据巴贝尔REPL的说法,他们被描述为
var arrowObject = {
name: 'arrowObject',
printName: function printName() {
console.log(undefined);
}
};
Run Code Online (Sandbox Code Playgroud)
和
var functionObject = {
name: 'functionObject',
printName: function printName() {
console.log(this);
}
};
Run Code Online (Sandbox Code Playgroud)
为什么没有arrowObject.printName();被arrowObject …
javascript object-literal ecmascript-6 babeljs arrow-functions
我想把我的javascript代码"更新"到新的ES6标准,所以我看看现在如何编写函数并在我的全局函数上试用它,在"旧"es5中读取如下所示
function logMessage(message) {
document.getElementById("logs").innerHTML = document.getElementById("logs").innerHTML + `<li class="item-padding"> ${message} </li>`
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我没错,es6的正确"转换"将是这样的:
logMessage = message => {
etc
}
Run Code Online (Sandbox Code Playgroud)
但我的ESLint告诉我,我的logMessage没有定义,我的控制台出错了,我错过了什么吗?我必须在logMessage之前声明var,let或const吗?
我不知道它是否重要,但我也想将这个函数从文件One导出到文件二,并在文件二中的另一个函数中使用函数logMessage,这样做有什么我必须记住的吗?
谢谢你的帮助!
编辑:谢谢大家的答案帮了我很多,我的问题解决了!
是否有理由不在中间件中为处理程序使用箭头而不是常规函数表达式?
app.use(mountSomething())
router.use(mountSomethingElse())
app.get('/', (req,res,next)=> {
next();
})
route.get('/path', (req,res,next)=>{
res.send('send')
})
Run Code Online (Sandbox Code Playgroud) 我为User实体制作了一个Mongoose数据库模式,并希望在updated_at字段中添加当前日期.我正在尝试使用.pre('save', function() {})回调,但每次运行它时都会收到一条错误消息,告诉我this未定义.我也决定使用ES6,我想这可能是一个原因(尽管一切正常).我的Mongoose/Node ES6代码如下:
import mongoose from 'mongoose'
mongoose.connect("mongodb://localhost:27017/database", (err, res) => {
if (err) {
console.log("ERROR: " + err)
} else {
console.log("Connected to Mongo successfuly")
}
})
const userSchema = new mongoose.Schema({
"email": { type: String, required: true, unique: true, trim: true },
"username": { type: String, required: true, unique: true },
"name": {
"first": String,
"last": String
},
"password": { type: String, required: true },
"created_at": { type: Date, default: Date.now …Run Code Online (Sandbox Code Playgroud) let role = {
test: (variable) => {
// How do I call toLog(variable) from here?
},
toLog: (variable) => {
console.log(variable);
}
};
Run Code Online (Sandbox Code Playgroud)
我想在test()函数中调用toLog()函数,但我不知道如何.
我想func2从函数的示例函数中调用函数func1.有人可以提出一种方法来实现这一目标吗?
class A
{
public func1()
{
let sample = function()
{
//call func2... but how?
}
}
public func2()
{
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
我更喜欢这个:
const foo = x => x + 1;
Run Code Online (Sandbox Code Playgroud)
对此:
function foo(x) {
return x + 1;
}
Run Code Online (Sandbox Code Playgroud)
是否有eslint规则来强制执行此操作?
我开始学习Vue.js和ECMA6语法,我在教程中看到了这一点:
methods: {
someMethod: function() {
console.log(this) // this works
}
}
Run Code Online (Sandbox Code Playgroud)
然后我认为语法可能是:
methods: {
someMethod: () => {
console.log(this) // this undefined
}
}
Run Code Online (Sandbox Code Playgroud)
但这有效:
methods: {
someMethod () {
console.log(this) // this works
}
}
Run Code Online (Sandbox Code Playgroud)
可以解释差异和ECMA5的语法吗?
首先我尝试了这个 -
const profile = {
name: 'Alex',
getName: function(){
return this.name;
}
};
Run Code Online (Sandbox Code Playgroud)
哪个工作正常.现在我用胖箭尝试了同样的事情.在那种情况下,"这个"未定义.
const profile = {
name: 'Alex',
getName: () => {
return this.name;
}
};
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误
TypeError:无法读取未定义的属性"name"
我学到的是,胖箭头语法更好地处理隐含的"this".请解释为什么会发生这种情况.
javascript ×9
ecmascript-6 ×8
node.js ×2
babeljs ×1
eslint ×1
express ×1
function ×1
mongodb ×1
mongoose ×1
this ×1
typescript ×1
vue.js ×1