经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
关于 JS this
来源:cnblogs  作者:gaobowen  时间:2019/11/7 20:26:29  对本文有异议

关于 JS this

1. this 与 普通函数

普通函数 this 永远指向它的调用对象

  1. var obj = {
  2. a:10,
  3. b:{
  4. a:20,
  5. fn:function(){
  6. console.log(this.a);
  7. }
  8. }
  9. }
  10. obj.b.fn(); //=> 20

对象 b 调用 fn 函数 this.a 为 20。

  1. var obj2 = {
  2. a:10,
  3. b:{
  4. fn:function(){
  5. console.log(this.a);
  6. }
  7. }
  8. }
  9. obj2.b.fn(); //=> undefined

对象 b 中无 a 字段, this.a 为 undefined。

  1. var obj3 = {
  2. a:10,
  3. b:{
  4. a:20,
  5. fn:function(){
  6. console.log(this);
  7. console.log(this.a);
  8. }
  9. }
  10. }
  11. var j = obj3.b.fn;
  12. j(); //=> window, undefined

这里直接获取里fn对象,没有通过对象去调用。非严格模式下,this 默认指向全局对象window。

  1. var obj4 = {
  2. a:10,
  3. b:{
  4. a:20,
  5. fn:function(){
  6. "use strict";
  7. console.log(this);
  8. console.log(this.a);
  9. }
  10. }
  11. }
  12. var g = obj4.b.fn;
  13. g(); //=> undefined, TypeError: Cannot read property 'a' of undefined

而严格模式下, this 为 undefined。

2. this 与 bind、call、apply

function.bind(thisArg[, arg1[, arg2[, ...]]])?最简单的用法是创建一个函数,不论怎么调用,这个函数都有同样的?this?值。
function.call(thisArg, arg1, arg2, ...)、function.apply(thisArg, [argsArray]) call、apply 的作用类似都是为调用函数指定this。

  1. function fn() {
  2. return this.user;
  3. }
  4. console.log(fn.call({ user: "li" })); //=> li
  5. console.log(fn.apply({ user: "wang" })); //=> wang
  6. var bfn = fn.bind({ user: "gao" });
  7. console.log(bfn()); //=> gao
  8. console.log(bfn.call({ user: "liu" })); //=> gao
  9. console.log(bfn.apply({ user: "liu" })); //=> gao
  10. console.log(bfn.bind({ user: "liu" })()); //=> gao
  11. var obj = { user: 'zhang', f: fn, g: bfn };
  12. console.log(obj.f(), obj.g()); //=> zhang, gao

对 bind 产生的函数使用,再使用 call、apply、bind, 指向的 this 仍然是初次bind的值。

  1. function list() {
  2. return Array.prototype.slice.call(arguments);
  3. }
  4. console.log(list(1, 2, 3)); //=> [1, 2, 3]
  5. function list2(){
  6. if(arguments){
  7. arguments.__proto__.slice = Array.prototype.slice;
  8. return arguments.slice();
  9. }
  10. }
  11. console.log(list2(4, 5, 6)); //=> [4, 5, 6]
  12. var leading37List = list.bind(null, 37);
  13. console.log(leading37List()); //=> [37]
  14. console.log(leading37List(1, 2, 3)); //=> [37, 1, 2, 3]

在函数代码中,特殊对象 arguments 无需明确指出参数名,就能访问它们。arguments 自带 length 属性, Array.prototype.slice.call(arguments) 可以理解成 arguments.slice()

3. this 与 箭头函数

由于箭头函数不绑定this,它会捕获其所在(即定义的位置)上下文的this值, 作为自己的this值。

  1. var obj = {
  2. a: 10,
  3. b: function () {
  4. console.log(this.a, this)
  5. },
  6. c: () => console.log(this.a, this),
  7. }
  8. obj.b(); //=> 10, Object{...}
  9. obj.c(); //=> undefined, window{...}
  10. obj.c.apply({ a: 'apply' }); //=> undefined, window{...}
  11. obj.c.call({ a: 'call' }); //=> undefined, window{...}
  12. obj.c.bind({ a: 'bind' })(); //=> undefined, window{...}

call() 、 apply() 、 bind() 方法对于箭头函数来说只是传入参数,对它的 this 毫无影响。

  1. function fn() {
  2. this.a = 20;
  3. this.b = () => console.log(this.a, this);
  4. }
  5. console.log((new fn()).b()) //=> 20, fn?{a: 20, b: ?}

箭头函数不会创建自己的this,它只会从自己的作用域链的上一层继承this。

  1. var lam = (...r) => {
  2. console.log(r);
  3. }
  4. lam(1,2,3); //=> Array : [1, 2, 3]

箭头函数没有arguments,取而代之用rest参数…解决。

  1. var B = ()=>{
  2. value:1;
  3. }
  4. var b = new B(); //=> TypeError: B is not a constructor

箭头函数作为匿名函数,没有构造函数,不能使用new。

4. this 与 return

如果返回值是 引用类型 对象,那么this指向的就是那个返回的对象;
如果返回值是 非引用类型 对象那么this还是指向函数的实例。

4.1 返回引用对象

  1. function fn()
  2. {
  3. this.user = '123';
  4. return {};
  5. }
  6. var a = new fn;
  7. console.log(a.user); //=> undefined

4.2 返回function对象

  1. function fn()
  2. {
  3. this.user = '123';
  4. return function(){};
  5. }
  6. var a = new fn;
  7. console.log(a.user); //=> undefined

4.3 返回数字,值对象

  1. function fn()
  2. {
  3. this.user = '123';
  4. return 123;
  5. }
  6. var a = new fn;
  7. console.log(a.user); //=> 123

4.4 返回undefined

  1. function fn()
  2. {
  3. this.user = '123';
  4. return undefined;
  5. }
  6. var a = new fn;
  7. console.log(a.user); //=> 123

4.5 返回null

  1. function fn()
  2. {
  3. this.user = '123';
  4. return null;
  5. }
  6. var a = new fn;
  7. console.log(a.user); //=> 123

返回目录

原文链接:http://www.cnblogs.com/gaobw/p/11807098.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号