JavaScript基本问题. [英] Javascript basic question.

查看:112
本文介绍了JavaScript基本问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是javascript的初学者.

Hi All,

I am beginner to javascript.

    var ButtonHandler = function()
    {
        var me = this;
        
        this.click = function()
        {
            alert("Click");
            me.meClick(); // How It can find "me"!!?
        };
        this.meClick = function()
        {
            alert("meClick");
        };
    }; 
    var b = new ButtonHandler(); // me created and destroyed.

    function onBtnclick()
    {
        b.click();
        //ButtonHandler.prototype.click();
    }
}



我将onBtnclick绑定到按钮单击.

我的困惑是在创建 var b 时,"me"已创建并销毁.

但是当我单击按钮时,它如何找到我"?.


对不起,我的英语
谢谢大家.



I bind onBtnclick to button click.

My confusion is at creation of var b "me" is created and destroyed.

but when i click button,How it finds "me"?.


Sorry for my english
Thanks all.

推荐答案

这就是所谓的闭包 [ ^ ].在定义要分配给this.click的函数时,周围环境中定义的所有变量都可以从该函数定义中获取.

在此处查看: http://www.javascriptkit.com/javatutors/closures.shtml [
This is what is called a closure[^] in computer science. As the function is being defined that is being assigned to this.click all the variables that are defined in the surrounding environment are reachabel from within that function definition.

Seeh here: http://www.javascriptkit.com/javatutors/closures.shtml[^]

— Manfred


在创建变量b时创建并销毁了"me".
"me"何时何地被销毁?

只要"b"可用,对象就存在,它的成员也是如此.对? me没有被破坏,因此它存在.
at creation of var b "me" is created and destroyed.
When and where did ''me'' was destroyed?

As long as ''b'' is available, object exists and so does it''s members. Right? me was not destroyed and so it exists.


这篇关于JavaScript基本问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆