Could you please explain whats the mean of
first[dot]second like declaration in javascript rules
first.second = (function () {
//...
})();
it means first
is an object and second
is a property of that object.
it can be defined as follows too;
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
That is actualy name spacing and we basically use objects to do that
Read this https://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/
In your code first
will be a object and second
will be a property of it.
But using your code directly may cause a error as the object first
is not yet initialized by you
So you should initialize it first as below
var first = {};
Suppose you have object obj and you want to access its inner child property, look at below example:
var obj= {
innerProp:{
innerinnerProp:{
hello:'inner world',
hello2: 'testing the world'
}
},
hello: 'outside world'
}
to know the value of hello2.
console.log(obj.innerProp.innerinnerProp.hello);
The one you posted above is called an object
and you can access object's properties with help of dot notation
said above. When you have another object as property, I mean nested objects, it can be accessed with second dot, thirs dot
etc based on its level.
For an instance,
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);
Dot-notation is most commonly used in JS to access properties of objects, which means that the property name is given after a full stop character.
For example:
var myCar = new Object();
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;
console.log(myCar.color) // undefined
console.log(myCar.model) // Mustang
Ref: Property accessors, JS Dot Notation
Hope this helps :)