函数中的this和var之间的区别 [英] Difference between this and var in a function

查看:87
本文介绍了函数中的this和var之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在创建的javascript类。此类具有私有和公共函数/属性。我对私人和公众的理解是这个是公开的, var 是该函数及其成员的私有。但是,在本地函数 buildFramework()中,当我调用 var settings.currentView 时,我收到错误:

I have a javascript class that I'm creating. This class has private and public functions/properties. My understanding of private and public was that this was public and var was private to that function and its members. However, within the local function buildFramework(), when I call the var settings.currentView I get the error:


settings.currentView 未定义

我的问题是,这个 var 在函数范围内及其成员以及全局范围?

My question is, what is the difference between this and var in the scope of a function and its members as well as the global scope?

namespace('example');
example.InstagramViewer = function (options) {
    // this works when called within buildFramework()
    this.settings = $.extend({
        currentView: 'grid'
    }, options);

    // this doesn't work when called within buildFramework()
    var settings = $.extend({
        currentView: 'grid'
    }, options);

    var viewer;

    this.init = function () {
        buildFramework();
    };

    var buildFramework = function() {
        viewer = $(viewerWrapper).append('<div id="instagramViewer" class="' + settings.currentView + '"></div>'); // this doesn't work
        viewer = $(viewerWrapper).append('<div id="instagramViewer" class="' + this.settings.currentView + '"></div>'); // this does work
    };
}

并且这样调用......

and called like so...

$(function () {
    var viewer = new connectionsAcademy.publicWebsite.web.js.teenWebsite.InstagramViewer();
    viewer.init();
});


推荐答案

这里有很多不同之处。

There are a lot of differences here.

首先,'this'是一个关键字,它是对调用对象的引用。如果按原样调用函数,则窗口将是调用对象,并且您使用关键字this设置全局变量。

First, 'this' is a keyword which is a reference to the calling object. If you call your function as is, the window will be the calling object and you are setting a global variable by using the the keyword 'this'.

InstagramViewer();
console.log(window.settings);

但是,如果你通过另一个对象调用你的函数,窗口将不再是调用对象而是设置全局变量时,您将在该对象上设置成员变量。

However, if you call your function through another object, window will no longer be the calling object and instead of setting a global variable you will be setting a member variable on that object.

var obj = {InstagramViewer: InstagramViewer};
obj.InstagramViewer();
console.log(obj.settings);

在这两种情况下使用'var'没有区别。 'var'与'this'的工作方式不同,因为除了我们称之为函数的暂存器之外,它不会影响任何对象。只有该函数可以访问其暂存器(除非您创建某种类型的闭包,它在此便笺簿上公开变量)。这就是为什么您可以将使用'var'定义的变量视为私有的原因。

using 'var' in both these cases makes no difference. 'var' works different then 'this' because it affects no object besides what we will call the function's scratchpad. Only the function can access its scratchpad (unless you create some kind of closure which exposes a variable on this scratch pad). This is why you can think of a variable defined with 'var' as being private.

使用'var'与处理'delete'时的'this'不同。 'delete'不适用于使用'var'声明的变量,但它会在'this'或任何其他对象上定义变量时使用。

using 'var' is also different then 'this' when dealing with 'delete'. 'delete' will not work on a variable declared with 'var', but it will when defining a variable on 'this' or any other object.

var F = function() {
    this.foo = 'foo';
    var bar = 'bar';

    delete(bar);
    delete(this.foo);

    alert(bar);
    alert(this.foo);
};


F();

我的解释粗略,但这是一个很大的主题,在这方面无法轻易解释。我强烈建议你阅读这本书 http://shop.oreilly.com/product/9780596000486.do 。特别是第7章。

My explanation is crude, but this is a big subject that can not be easily explained in this context. I highly recommend you read this book http://shop.oreilly.com/product/9780596000486.do. Especially chapter 7.

这篇关于函数中的this和var之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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