GoogleGuava之字符串操作


立即下载 一生流水
2025-11-16
te 对象 person 方法 st.call st 函数 var 调用 JavaScript
77.8 KB

黑马程序员郑州中心 编著
黑马程序员:认识 JavaScript 中的 this
很多同学在学习编程时 , 特别是零基础的同学 , 在学到 web 阶段 JavaScript 课程过程
中 , 对于关键字 this, 很不理解 , 不知道该如何简单明了的理解并学习这个知识点 , 那么今
天我们就一起来分析一下 JavaScript 中 this 关键字到底是个什么鬼
this 是 Javascript 语言的一个关键字。那么 this 到底是真名呢?简而言之, this 指的
是函数的调用者。看一个例子:
var name = " 张三 ";
function test() {
console.log(this.name);
}
var person = new Object();
person.name = " 李四 ";
test.call();
test.call(person);
大家看一下结果是什么。第一个 log 是张三,第二个是李四。
我们分析一下, test.call(); 是 window 调用了 test 方法,所以在第一次执行 test 函数
时,this 指的是 window 对象,第二次调用 test 方法是 test.call(person); 这时调用 test 函
黑马程序员郑州中心 编著
数的对象时 person ,此时函数中的 this 指的就是 person 对象。
我们再看一个例子:
function test2() {
this.x = 1;
}
var o = new test2();
console.log(o.x);
log 结果是 1。
通过构造方法生成一个新的对象 o,此时该构造方法中的 this 指的就是这个新生成的对象。
那么现在你对” this 指的是函数的调用者”这句话理解了吗?


te/对象/person/方法/st.call/st/函数/var/调用/JavaScript/ te/对象/person/方法/st.call/st/函数/var/调用/JavaScript/
-1 条回复
登录 后才能参与评论
-->