簡體   English   中英

用this或var聲明變量?

[英]Declaring variables with this or var?

使用thisvar聲明變量之間有什么區別?

var foo = 'bar'

要么

this.foo = 'bar'

什么時候使用thisvar

編輯:是否有一個簡單的問題,我可以在決定是否要使用varthis時詢問自己

如果是全球性的代碼 (代碼是沒有任何功能的一部分),那么您要創建與兩個片段全局對象上的屬性,因為this全局代碼指向全局對象。

這種情況的不同之處在於,當使用var語句時,無法刪除該屬性,例如:

var foo = 'bar';
delete foo; // false
typeof foo; // "string"

this.bar = 'baz';
delete bar; // true
typeof bar; "undefined"

注意:上面的代碼片段在Firebug控制台中的行為會有所不同,因為它使用eval運行代碼,並且在Eval代碼執行上下文中執行的代碼允許刪除使用var創建的標識符,請在此處嘗試)

如果代碼是函數的一部分,您應該知道this關鍵字與函數作用域無關,是隱式設置的保留字,具體取決於函數的調用方式,例如:

1 - 當函數作為方法調用時(該函數作為對象的成員調用):

obj.method(); // 'this' inside method will refer to obj

2 - 正常的函數調用:

myFunction(); // 'this' inside the function will refer to the Global object
// or 
(function () {})();

3 - 使用新運算符時:

var obj = new Constructor(); // 'this' will refer to a newly created object.    

您甚至可以使用callapply方法顯式設置this值,例如:

function test () {
  alert(this);
}
test.call("hello!"); //alerts hello!

您還應該知道JavaScript只有函數作用域 ,而使用var語句聲明的變量只能在同一個函數或下面定義的任何內部函數中訪問。

編輯:查看您發布到@ David的答案的代碼,讓我評論:

var test1 = 'test';  // two globals, with the difference I talk
this.test2 = 'test'; // about in the beginning of this answer

//...

function test4(){
    var test5 = 'test in function with var'; // <-- test5 is locally scoped!!!
    this.test6 = 'test in function with this'; // global property, see below
}

test4(); // <--- test4 will be called with `this` pointing to the global object
         // see #2 above, a call to an identifier that is not an property of an
         // object causes it

alert(typeof test5); // "undefined" since it's a local variable of `test4` 
alert(test6); // "test in function with this"

您無法訪問函數外部的test5變量,因為它是本地作用域的,並且只存在該函數的作用域。

編輯:回復您的評論

為了聲明變量,我鼓勵你總是使用var ,這就是為什么做的。

當您開始使用構造函數 ,對象和方法時, this值的概念將變得有用。

如果使用var ,則變量的作用域為當前函數。

如果你使用this ,那么你分配一個值上任何一個屬性this是(這是可以或者(如果在方法被調用的對象new關鍵字已使用)中創建的對象。

var foo = 'bar'

這將foo變量的范圍foo在包裝它的函數或全局范圍。

this.foo = 'bar'

這將foo變量的范圍擴展到this對象,它就像這樣做:

window.foo = 'bar';

要么

someObj.foo = 'bar';

你問題的第二部分似乎是什么this目標,那就是通過函數在運行的情況下確定的東西。 你可以改變什么this是通過使用應用方法的所有功能都有 您還可以this變量的默認值設置為全局對象以外的對象,方法是:

someObj.foo = function(){
  // 'this' is 'someObj'
};

要么

function someObj(x){
  this.x=x;
}
someObj.prototype.getX = function(){
  return this.x;
}
var myX = (new someObj(1)).getX(); // myX == 1

如果要像在典型函數中那樣定義簡單的局部變量,則使用var : -

function doAdd(a, b)
{
  var c = a + b;
  return c;
}

var result = doAdd(a, b);

alert(result);

但是,當在函數上使用call時, this具有特殊含義。

function doAdd(a, b)
{
   this.c = a + b;
}

var o = new Object();
doAdd.call(o, a, b);
alert(o.c);

您注意到在doAdd上使用call時的第一個參數是之前創建的對象。 在doAdd的執行中, this將引用該對象。 因此它在對象上創建了一個c屬性。

通常,雖然將函數分配給對象的屬性,如下所示: -

function doAdd(a, b)
{
   this.c = a + b;
}

var o = new Object();
o.doAdd = doAdd;

現在該函數可以使用。 符號:-

o.doAdd(a, b);    
alert(o.c);

有效地o.doAdd(a, b)o.doAdd.call(o, a, b)

var foo = 'bar'; // 'var can be only used inside a function

this.foo = 'bar' // 'this' can be used globally inside an object

在構造函數中,您可以使用var來模擬私有成員,並使用它來模擬公共成員:

function Obj() {
  this.pub = 'public';
  var priv = 'private';
}

var o = new Obj();
o.pub;    // 'public'
o.priv;   // error

下面解釋了這個var的例子:


function Car() {
   this.speed = 0;

   var speedUp = function() {
      var speed = 10; // default
      this.speed = this.speed + speed; // see how this and var are used
   };

   speedUp();
}


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM