簡體   English   中英

Javascript:使用的區別是什么? 和:用於在函數或對象中指定變量的運算符?

[英]Javascript: What is the difference between usage of . and : operators for specifying a variable in a function or object?

對於在javascript函數中使用靜態變量的方法,我發現了兩種使用的方法。 和:運算符。 使用時。 運算符,我們必須使用“ f.variable”指定變量,而在使用時:我們必須使用“ this.variable”。 這兩個運算符的用法有什么區別。

function f(){
  f.a += 1;
  this.b += 1;
  console.log("f.a: ", f.a);
  console.log("this.b: ", this.b);
}
f.a = 0;
f:b = 0;

我們也不能使用:當在函數之外使用該變量時,例如:

function g(){
  f:b = 0; //this works fine.
  var c = f:b; //raises error invalid label.
  console.log(f:b);//but this raises an error missing ')'.
}

當我們使用var創建對象時,情況也是如此。

var obj = {
 a: 2,
 b: 3
}
//accessing a and b is done using obj.a & obj.b
//but here
obj:a = 4;
console.log(f.a); // this gives 2
//and similarly using obj:a as rhs value gives error.

實際如何使用這兩個運算符。

編輯:這兩種類型的變量之間有什么區別。

這個:

f:b = 0;

在表達式語句之前, b = 0;解釋為標簽 “ f” b = 0; 對象文字語法中使用“:”將屬性名稱表達式與其值表達式分開。 否則,它不用於引用對象的屬性。

暫無
暫無

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

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