惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
小众软件
小众软件
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
Recorded Future
Recorded Future
雷峰网
雷峰网
WordPress大学
WordPress大学
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
T
Tor Project blog
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Spread Privacy
Spread Privacy
G
Google Developers Blog
Security Latest
Security Latest
MongoDB | Blog
MongoDB | Blog
T
Threatpost
I
InfoQ
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
S
Security Affairs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
aimingoo的专栏
aimingoo的专栏
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
D
Docker
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
PCI Perspectives
PCI Perspectives
Microsoft Azure Blog
Microsoft Azure Blog
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
Cloudbric
Cloudbric
A
About on SuperTechFans
F
Fortinet All Blogs

博客园 - Tobin

41岁的大龄程序员,苟着苟着,要为以后做打算了 UC_Center整合单点登录后远程注册不激活问题的解决办法 AspExe - a small ASP.NET compiler and executor for document generation 在as3中Embed(绑定)flash动画元素 - Tobin - 博客园 Embedding Resources with AS3 Configure the max limit for concurrent TCP connections [转]翻译:使用.net3.5的缓存池和SocketAsyncEventArgs类创建socket服务器 强制将IE,Chrome设置为指定兼容模式来解析(转) - Tobin - 博客园 工商银行,千万别用,转账不成功一样收手续费 MySQL vs NoSQL 效率与成本之争(转) 使用ASP.NET Global.asax 文件(转) [MySql识记]create utf8 database - Tobin npgsql连接postgresql数据库 由浅到深了解JavaScript类[转过来的收藏] 哪个美女最漂亮,自己写的js图片自适应切换 javascript操作cookie实例 [图解] 你不知道的 JavaScript - “this”(转) 对与list<>泛型的一些操作方法 关于游戏开发中的A*/A-star的寻路算法的问题
javascript改变this指针
Tobin · 2008-07-15 · via 博客园 - Tobin

今天写代码时遇到一个问题,this指针的问题,先把代码贴上

<script type="text/javascript">
window.onload
=function()
{
    
var m=new main('aaaa');
    m.run();
}

var main=function(param)
{
     
this.param=param;

     
this.run=function()
     
{
     setTimeout(
this.Change,3000);
     }


     
this.Change=function()
     
{
     alert(
this.param);
     }

}

</script>

这个代码看起来很清晰,但是会报错,原因我想大概就是在隔了三秒执行Change方法时找不到this.param,this.Change的this就指向了window,不是实例m了,整了半天没有整明白,没办法只好上网求助,结果有高手给除了解决方法,修改this指针。再来看新代码:

    <script type="text/javascript">
window.onload
=function()
{
    
var m=new main('aaaa');
    m.run();
}

var main=function(param)
{
     
this.param=param;
     
this.run=function()
     
{
     
var self=this;
     setTimeout(self.Change.setThis(self),
3000);
     }

     
this.Change=function()
     
{
     alert(
this.param);
     }

     Function.prototype.setThis 
= function(object) 
     

        
var __method = this
        
return function() 
        

        __method.apply(object, arguments); 
        }
 ;
     }
;
}

</script>

其中添加了一段修改指正的代码,修改指针代码:

 Function.prototype.setThis = function(object) 
     { 
        var __method = this; 
        return function() 
        { 
        __method.apply(object, arguments); 
        } ;
     };

使用的时候要定义一个变量self=this; 用self来调用。最主要的是不理解apply到底是做啥子用的,只能先记下来,以后慢慢理解!