在javascript语法中使用带有多个点符号

时间:2016-11-17 07:11:22

标签: javascript javascript-objects notation


你能解释一下它的意思吗?
第一个[dot]秒像javascript规则中的声明

first.second = (function () {
    //...
})();

6 个答案:

答案 0 :(得分:0)

首先是你可以通过名字获得子对象的对象。

例如:

var human = { 
    firstName: 'Joe',
    age: 30
}

您可以通过指定所需变量的名称来获得年龄。

var age = human.age
// or first name
var name = human.firstName

答案 1 :(得分:0)

这是实际的名称间距,我们基本上使用对象来做那个

阅读此https://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/

在您的代码中Function IfFileExist(strPath As String, strFileName As String) IfFileExist = False If Len(Dir(strPath & "\" & strFileName)) > 0 Then IfFileExist = True End If End Function 将是一个对象,first将成为它的属性。

但直接使用您的代码可能会导致错误,因为您尚未初始化对象second

所以你应该首先将其初始化为

first

答案 2 :(得分:0)

表示first是一个对象,second是该对象的属性。

它也可以定义如下;

var first = {};// {} -is JS object notation
first.second = function(){
   alert('test');
};

//or
var first = {
  second : function(){
     alert('test');
  }
};


//invoke the function
first.second();// prompt alert message

simple documentation

答案 3 :(得分:0)

假设您有对象obj并且您想要访问其内部子属性,请查看以下示例:

 var obj= {
    innerProp:{
    innerinnerProp:{
    hello:'inner world',
    hello2: 'testing the world'
                   }
              },
    hello: 'outside world'
  }

知道hello2的值。

的console.log(obj.innerProp.innerinnerProp.hello);

答案 4 :(得分:0)

您在上面发布的内容称为object,您可以在上述dot notation的帮助下访问对象的属性。如果你有另一个对象作为属性,我的意思是嵌套对象,可以根据它的级别使用second dot, thirs dot等访问它。

例如,



var first = {
   second: {
     third: 'test2'
   },
   prop: 'test1'
};

// You can access the above with below dot notations
console.log(first.prop);
console.log(first.second);
console.log(first.second.third);




答案 5 :(得分:0)

点符号在JS中最常用于访问对象的属性,这意味着属性名称是在完整停止字符后给出的。

例如:

var myCar = new Object();
    myCar.make = "Ford";
    myCar.model = "Mustang";
    myCar.year = 1969;

console.log(myCar.color) // undefined
console.log(myCar.model) // Mustang

参考:Property accessorsJS Dot Notation

希望这会有所帮助:)