资讯专栏INFORMATION COLUMN

the deadline of JavaScript's this

chinafgj / 2968人阅读

摘要:在用处千千万,基于自己研究和认识,今天做一个了断。可以取所属对象的上下文的方法称为公共方法,可以使属性,方法变成公开的属性方法在构造函数,方法中用到。内部函数调用的时候,只能搜索到其活动对象为止,不可能直接访问外部函数中的变量。

this

this在JavaScript用处千千万,基于自己研究和认识,今天做一个了断。

全局,匿名函数调用

对象方法调用

闭包总指向上一级

构造函数中,指向本身

引用时候,指向Windows

apply调用

全局(Global context)

</>复制代码

  1. In the global execution context (outside of any function),
    this refers to the global object, whether in strict mode or not.

</>复制代码

  1. 当在全局环境执行的时候,无论“严格模式”or“非严格模式”,this指向全局对象

</>复制代码

  1. console.log(this.document === document); // true
  2. // In web browsers, the window object is also the global object:
  3. console.log(this === window); // true
  4. this.a = 37;//this.a 等价于 var a = 37;
  5. console.log(window.a); // 37
函数中严与非严格有区别

</>复制代码

  1. function f1(){
  2. return this;
  3. }
  4. f1() === window; // global object

严格

</>复制代码

  1. function f2(){
  2. "use strict"; // see strict mode
  3. return this;
  4. }
  5. f2() === undefined;
方法调用

方法:当一个函数被保存为对象的一个属性的时候。

</>复制代码

  1. var num = {
  2. sum: 0,
  3. add: function(x,y){
  4. this.sum = x + y;
  5. console.log(this);
  6. }
  7. }
  8. num.add(2,3);
  9. console.log(num.sum);

this 可以取所属对象的上下文的方法称为公共方法,可以使属性,方法变成公开的属性方法(在构造函数,方法中用到)。

构造器调用

需要使用new来调用,函数创建一个对象链接到prototype,this会绑定到那个新的对象上。

</>复制代码

  1. var Person= function(name){
  2. this.name= name;
  3. }
  4. Person.prototype.showname= function(){
  5. console.log(this);
  6. return this.name;
  7. }
  8. var p = new Person("duke");
  9. console.log("duke"+":"+p.showname());
函数调用

函数调用的时候会自动取得两个特殊的变量:this,arguments。js内部函数调用的时候,只能搜索到其活动对象为止,不可能直接访问外部函数中的变量。

解决方案:

如果该方法定义一个变量并给他赋值为this,那么内部函数就可以通过那个变量访问到this,我们可以把那个变量定义为that。

</>复制代码

  1. var myfun= {
  2. num: 1,
  3. fadd: function(x){
  4. this.num= x+3
  5. }
  6. }
  7. myfun.double= function(){
  8. var that = this;
  9. console.log(that);
  10. var d= function(){
  11. that.num= 90;
  12. that.num2= 1999;//可以用作添加属性
  13. console.log(that);
  14. }
  15. d();
  16. }
  17. // 这个案例说明没有外部变量引入到内部函数中
  18. myfun.three= function(){
  19. console.log(this);
  20. console.log("three"+this.num);
  21. var that = this;//key point
  22. var t = function(){
  23. console.log("this"+this);
  24. console.log("that"+that);
  25. console.log("inner"+this.num);
  26. console.log("inner"+that.num);
  27. }
  28. t();
  29. }
  30. myfun.fadd(4);
  31. console.log(myfun.num);
  32. myfun.double();
  33. console.log("double"+myfun.num);
  34. myfun.three();
apply调用

接收两个参数,第一个绑定给this,第二个就是一个参数数组

apply,call用法

apply

javascript中的this应用

apply,call 延伸

</>复制代码

  1. Where a function uses the this keyword in its body,
    its value can be bound to a particular object in the call using the
    call or apply methods that all functions inherit from Function.prototype.

</>复制代码

  1. function add(c, d){
  2. return this.a + this.b + c + d;
  3. }
  4. var o = {a:1, b:3};
  5. // The first parameter is the object to use as
  6. // "this", subsequent parameters are passed as
  7. // arguments in the function call
  8. add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16
  9. // The first parameter is the object to use as
  10. // "this", the second is an array whose
  11. // members are used as the arguments in the function call
  12. add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34

用在类型检测

</>复制代码

  1. function bar() {
  2. console.log(Object.prototype.toString.call(this));
  3. }
  4. bar.call(7); // [object Number]
  5. // 用apply较多,使用范围广
  6. function bar() {
  7. console.log(Object.prototype.toString.apply(this));
  8. }
  9. bar.apply(7); // [object Number]
As a DOM event handler(dom事件处理)

</>复制代码

  1. When a function is used as an event handler,
    its this is set to the element the event fired from

</>复制代码

  1. 用作事件的处理,给元素绑定方法

</>复制代码

  1. // When called as a listener, turns the related element blue
  2. function bluify(e){
  3. // Always true
  4. console.log(this === e.currentTarget);
  5. // true when currentTarget and target are the same object
  6. console.log(this === e.target);
  7. this.style.backgroundColor = "#A5D9F3";
  8. }
  9. // Get a list of every element in the document
  10. var elements = document.getElementsByTagName("*");
  11. // Add bluify as a click listener so when the
  12. // element is clicked on, it turns blue
  13. for(var i=0 ; i
  14. 参考developer.mozilla

  15. 总结,个人见解,欢迎批评指正

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85829.html

相关文章

  • Socket Error 104 bug

    摘要:概述技术栈错误详情报警机器人经常有如下警告过程确定报错位置有日志就很好办首先看日志在哪里打的从三个地方入手我们自己的代码没有的代码从上下来没有的代码在容器中执行 bug概述 技术栈 nginx uwsgi bottle 错误详情 报警机器人经常有如下警告: 1 2018-xx-xxT06:59:03.038Z 660ece0ebaad admin/admin 14 - - Sock...

    keithyau 评论0 收藏0
  • 简单易懂的ECMA规范导读1 that&#039;s this

    摘要:本文不是标准的中文翻译,也不是的入门教程,本文虽然以的常见问题切入,但并不适合想要快速了解这些问题的人才是快速了解问题的正解。尽量以英文原版为基础,为了流畅,可能会使用某些名词的中文翻译,但会将匹配的英文名词以此种样式中出现一次以避免误解。 简单易懂的ECMA规范导读1 序 最近混SF,恰巧又逢工作方面有了NodeJS的机会,迫切地有教别人怎么写JS的需求, 我发现JS这个东西其实...

    yintaolaowanzi 评论0 收藏0
  • 人工智能/数据科学比赛汇总 2019.6

    摘要:内容来自,人工智能数据科学比赛整理平台。大赛面向全球高校在校生开放,旨在提升高校学生对数据分析与处理的算法研究与技术应用能力,探索大数据的核心科学与技术问题,尝试创新大数据技术,推动大数据的产学研用,本次大赛鼓励高校教师参与指导。 内容来自 DataSciComp,人工智能/数据科学比赛整理平台。Github:iphysresearch/DataSciComp 本项目由 ApacheC...

    gyl_coder 评论0 收藏0
  • React:"don&#039;t fuck it up like Google did

    摘要:核心开发人员大神在开了个,用来征询社区对的建议。而且的工程师并没有因此止步,他们在文档中又告诉开发者,不仅仅要把写到中,也应该写到中。无论怎么使用自定义语法,也不应该影响这种好处,即使最终实现看起来有一些怪异。 React 核心开发人员 sebmarkbage 大神在 GitHub 开了个 issues,用来征询社区对 JSX 2.0 的建议。 showImg(https://segm...

    Cristalven 评论0 收藏0
  • 聊聊jdk httpclient的ConnectionPool

    摘要:调用计算的时间,这个方法会清理移除并过期的连接除了清理过期的连接外,还通过间接触发,去清理关闭或异常的连接 序 本文主要研究一下jdk httpclient的ConnectionPool HttpConnection HttpConnection.getConnection java.net.http/jdk/internal/net/http/HttpConnection.java ...

    Worktile 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<