0


Could you please explain whats the mean of
first[dot]second like declaration in javascript rules

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

6 Answers 6

0

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

simple documentation

Sign up to request clarification or add additional context in comments.

Comments

0

first is object that you can get subobjects by their names.

For example:

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

You can get age by specifing the name of the variable you want.

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

Comments

0

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 = {};

Comments

0

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);

Comments

0

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);

Comments

0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.