简体   繁体   English

Javascript:使用的区别是什么? 和:用于在函数或对象中指定变量的运算符?

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

For using a static variable in javascript functions I found out two ways, using . 对于在javascript函数中使用静态变量的方法,我发现了两种使用的方法。 and : operator. 和:运算符。 When using . 使用时。 operator we have to specify variable with "f.variable" and when using : we have to use "this.variable". 运算符,我们必须使用“ f.variable”指定变量,而在使用时:我们必须使用“ this.variable”。 what is the difference between the usage of these two operators. 这两个运算符的用法有什么区别。

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;

also we cannot use : when using that variable outside its function like: 我们也不能使用:当在函数之外使用该变量时,例如:

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 ')'.
}

Same is the case when we use var to create objects. 当我们使用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.

How are these two operators used actually. 实际如何使用这两个运算符。

EDIT: What is the difference between these two types of variables created. 编辑:这两种类型的变量之间有什么区别。

This: 这个:

f:b = 0;

is interpreted as a label , "f", before an expression statement, b = 0; 在表达式语句之前, b = 0;解释为标签 “ f” b = 0; . The ":" is used in object literal syntax to separate a property name expression from its value expression. 对象文字语法中使用“:”将属性名称表达式与其值表达式分开。 Otherwise, it is not used for referring to properties of objects. 否则,它不用于引用对象的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 中 != 和 !== 运算符有什么区别? - What is the difference between != and !== operators in JavaScript? JavaScript中的>>和>>>运算符有什么区别 - what is the difference between >> and >>> operators in JavaScript 在 javascript 中导出 function 或 object 有什么区别? - What is the difference between exporting a function or an object in javascript? 返回带有方法的对象的函数和带有方法的对象的变量之间有什么区别 - what is the difference between a function that returns an object with methods and a variable that is an object with methods 在javascript中,window.function(){}和var variable = function有什么区别? - In javascript, what is the difference between window.function(){} and var variable = function? javascript函数和javascript对象之间的主要核心区别是什么? - What is the main core difference between a javascript function and javascript object? Javascript Object、Property 和 Variable 有什么区别,它们都一样吗? - What is the difference between Javascript Object, Property and Variable, are they all the same? 在javascript对象中创建函数的不同方法有什么区别? - What is the difference between different ways to create a function inside javascript object? 这两种在JavaScript函数中利用arguments对象的方式有什么区别? - What is the difference between these two ways to leverage the arguments object in a JavaScript function? javascript中函数和对象的本质区别是什么? - What's substantive difference between function and Object in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM