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

using dot notation with more than one in javascript syntax


你能解释一下是什么意思吗
[dot]javascript 规则中的第一个[dot]第二行声明

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

首先是对象,您可以通过名称获取子对象。

例如:

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

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

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

这实际上是名称间距,我们基本上使用对象来做到这一点

读这个https://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/

在您的代码中,first 将是一个对象,second 将是它的 属性。

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

所以你应该先初始化如下

var first = {};

表示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

假设你有 object obj 并且你想访问它的内部 child 属性,请看下面的例子:

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

知道hello2的值。

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

您在上面发布的那个称为 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);

点号在 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 accessors, JS Dot Notation

希望对您有所帮助:)