什么时候函数返回结果以及什么时候JavaScript中的函数 [英] When function returns result and when function in JavaScript

查看:95
本文介绍了什么时候函数返回结果以及什么时候JavaScript中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多函数不返回结果而是函数.下面的示例显示getWindow函数返回函数.为什么不能只返回变量"win"?什么时候返回结果和什么时候函数?
谢谢.

I see a lot that function returns NOT the result but the function. The example below shows that function getWindow returns function. Why it can''t just return variable "win"? When I return result and when function?
Thank you.

var A = function(){};
A.prototype= 
{   
    getWindow : function()
    {
         var win = new B.window();         
         return ( 
                this.getWindow = function()
                {
                     return win;
                })(); 
         
    } 
}

推荐答案

这是一种Javascript模式,用于在对象上创建私有成员变量和特权getter函数.在您的特定示例中,它还使用延迟初始化来设置私有成员变量win的值.
您可以在道格拉斯·克罗克福德(Douglas Crockford)的网站上找到更多有关此模式的信息,网址为 http://www.crockford.com/javascript /private.html [ ^ ].
This is a Javascript pattern to create a private member variable and privileged getter function on an object. In your specific example, it is also using lazy initialization to set the value of the private member variable win.

You can read a bit more about this pattern on Douglas Crockford''s website at http://www.crockford.com/javascript/private.html[^].


这篇关于什么时候函数返回结果以及什么时候JavaScript中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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