this
时间:2021-07-01 17:25:30
收藏:0
阅读:0
created on 21/03/04
-
this是什么
- 任何函数本质上都是通过某个对象来调用的,未直接指定就是window
- 所有函数内部都有一个this
- this的值是调用函数的当前对象
-
如何确定this的值
- test():window
- p.test():p
- new test():新创建的对象
- p.call(obj):obj
function Person(color){ console.log(this); this.color = color; this.getColor = function(){ console.log(this); return this.color; }; this.setColor = function(color){ console.log(color); this.color = color; }; } Person("red"); //window var p = new Person("yello"); //p p.getColor(); //p var obj = {}; p.setColor.call(obj, "black"); //obj var test = p.setColor; test(); //window
评论(0)