我已经看到"this"关键字如何在函数中起作用?,但我不认为它回答了以下问题.
鉴于此代码:
var MyDate = function(date) {
this.date = date;
};
var obj1 = {
foo: new Date(),
bar: new MyDate(this.foo) // this.foo is undefined
};
var obj2 = {};
obj2.foo = new Date();
obj2.bar = new MyDate(this.foo); // this.foo is undefined
var obj3 = {
foo: new Date(),
bar: new MyDate(obj3.foo)
};
var obj4 = {};
obj4.foo = new Date();
obj4.bar = new MyDate(obj4.foo);
Run Code Online (Sandbox Code Playgroud)
为什么前两次尝试失败,但最后两次失败?如果this没有绑定到当前对象的文字,什么是它必然?
我正在学习Vue路由器.我想进行程序化导航(没有<router-link>).我的路由器和视图:
router = new VueRouter({
routes: [
{path : '/videos', name: 'allVideos', component: Videos },
{path : '/videos/:id/edit', name: 'editVideo', component: VideoEdit },
]
});
new Vue({
el: "#app",
router,
created: function(){
if(!localStorage.hasOwnProperty('auth_token')) {
window.location.replace('/account/login');
}
router.push({ name: 'allVideos' })
}
})
Run Code Online (Sandbox Code Playgroud)
因此,默认情况下,我推送到'allVideos'路线,在该组件内部,我有一个按钮和方法,可以重定向到''editVideo'按钮:
<button class="btn btn-sm btn-warning" @click="editVideo(video)">Edit</button>
Run Code Online (Sandbox Code Playgroud)
方法:
editVideo(video) {router.push({ name: 'editVideo', params: { id: video.id } })},
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是当我尝试在VideoEdit组件中获取id时,我得到$route.params.id了错误Uncaught ReferenceError:$ route未定义
也许是因为我现在不使用npm只是Vue和Vuerouter的cdn版本.有解决方案吗 谢谢!
更新:在Vue开发工具中使用btw我在组件中看到了$ route实例
更新:
var VideoEdit = Vue.component('VideoEdit', {
template: ` …Run Code Online (Sandbox Code Playgroud) 我正在尝试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
有人可以解释为什么用lambda表达式定义原型函数不起作用?我以为必须先问这个但是找不到它.
function Book(title, year) {
this.title = title;
this.year = year;
// define a function within the object, which works fine
this.printYear = () => console.log("instance function of an object: " + this.year);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用
Book.prototype.printTitle2 = () => {
console.log(this.title);
}
Run Code Online (Sandbox Code Playgroud)
这当然很好:
Book.prototype.printTitle = function() {
console.log(this);
console.log(this.title);
}
Run Code Online (Sandbox Code Playgroud) 我想了解ECMAScript 6中的箭头功能.
这是我在阅读时遇到的定义:
箭头函数具有隐式
this绑定,这意味着this箭头函数内部值的值与this定义箭头函数的范围中的值相同!
根据定义,我相信this一个arrow function应该包含箭头函数定义的相同块级别值.
码:
var test = {
id: "123123",
k: {
laptop: "ramen",
testfunc: () => console.log(this)
}
}
console.log(test.k.testfunc);
Run Code Online (Sandbox Code Playgroud)
但是,我从代码中得到了这个结果
function testfunc() {
return console.log(undefined);
}
Run Code Online (Sandbox Code Playgroud)
我以为我会得到的输出是:
{"laptop": "ramen"}
Run Code Online (Sandbox Code Playgroud)
如果我跑了
console.log(test.k.testfunc());
尝试正确学习箭头函数中的“this”关键字。
阅读并观看了一些视频后,我了解到使用常规函数,只要调用该函数,就会定义“this”关键字。
并且箭头函数内的“this”将根据“this”的值在您构建该函数的任何地方进行定义。
所以我打开控制台并玩了两个对象和两个功能。
我这样做了:
const reg = {
reg1: "reg1",
reg2: {
reg3: 'reg3',
regFunc: function(){console.log(this)}
}
}
const arrow = {
arrow1: "arrow1",
arrow2: {
arrow3: 'arrow3',
arrowFunc: () => {console.log(this)}
}
}
reg.reg2.regFunc()
VM712:5 {reg3: "reg3", regFunc: ƒ}
arrow.arrow2.arrowFunc()
VM712:13 Window {parent: Window, opener: null, top: Window, length: 1, frames: Window, …}
Run Code Online (Sandbox Code Playgroud)
对于 reg 对象,我得到它,因为它是在它向我显示的 reg2 对象内调用的。
但是对于箭头对象,箭头函数中的 this 关键字是在箭头内的 arrow2 对象内创建的。
那么为什么它的值不在箭头对象 arrow2 中呢?为什么是全局窗口?
首先我尝试了这个 -
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".请解释为什么会发生这种情况.
在下面的代码中,在 obj1 的对象字面量中,我假设两个函数中的“this”都将引用 obj1,但在粗箭头函数中,它不是。有人可以解释为什么吗?我会假设这些函数要么是等效的,要么在胖箭头函数中,'this' 将在词法上定义为 obj1。
var obj1 = {
name : 'name1',
standardFunction : function() {
console.log(this.name); // Refers to obj1
},
fatArrowFunction : () => { // Refers to the global object
console.log(this.name);
}
}
obj1.standardFunction();
obj1.fatArrowFunction();
Run Code Online (Sandbox Code Playgroud) 我发现'this'关键字似乎始终指向global在嵌套对象文字中使用箭头函数.
根据其他问题,下面的代码片段可以解释为箭头函数的'this'在词汇上下文中定义.
var c = 100;
var a = {c:5 , fn: () => {return this.c;} };
console.log(a.c); //100
Run Code Online (Sandbox Code Playgroud)
但是,我无法理解以下代码(嵌套对象文字):
var c = 100;
var a = {
c: 5,
b: {
c: 10,
fn: ()=> {return this.c;}
}
}
console.log(a.b.fn());// still 100, why not 5?
Run Code Online (Sandbox Code Playgroud)
我的意思是,如果从词汇语境方面考虑,不应该在abfn中的'this'指向一个?
为什么,无论对象嵌套多少级别,所有'this'实例都指向窗口还是全局?