栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

var self =这个?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

var self =这个?

这个问题并非特定于jQuery,而是通常特定于Javascript。核心问题是如何在嵌入式函数中“引导”变量。这是示例:

var abc = 1; // we want to use this variable in embedded functionsfunction xyz(){  console.log(abc); // it is available here!  function qwe(){    console.log(abc); // it is available here too!  }  ...};

此技术依赖于使用闭包。但这不能使用,

this
因为它
this
是一个伪变量,可能会随着范围的变化而动态变化:

// we want to use "this" variable in embedded functionsfunction xyz(){  // "this" is different here!  console.log(this); // not what we wanted!  function qwe(){    // "this" is different here too!    console.log(this); // not what we wanted!  }  ...};

我们能做什么?将其分配给某个变量,并通过别名使用它:

var abc = this; // we want to use this variable in embedded functionsfunction xyz(){  // "this" is different here! --- but we don't care!  console.log(abc); // now it is the right object!  function qwe(){    // "this" is different here too! --- but we don't care!    console.log(abc); // it is the right object here too!  }  ...};

this
在这方面不是唯一的:
arguments
是另一个应以相同方式处理的伪变量-通过别名。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号