Skip to main content
added 464 characters in body
Source Link
DontVoteMeDown
  • 21.5k
  • 10
  • 72
  • 113

You can declare an object like you done in your second question, it's valid because a function is an object too. Or other ways like:

var Name = {
    render: function() {
    }
}

Name.render();

Or with prototypeprototype:

function Name() {
}

Name.prototype.render = function() {
}
// or
Name.prototype = {
    getByID: function() {
    },
    setName: function() {
    }
}
var n = new Name();

All these snipets are a valid object declaration.

Your second question may answer the first ones. When you declare a function like this:

function Name() {
    function render() {
    }
}
var n = new Name();

It is like render() be a private method. if you call outside the function name n.render(), you will see an error thrown because render is not recognized. But if you change to this...

function Name() {
    this.render = function() {
    }
}

... then n.render() will work like render() being a public method. See this and this for further information about public and private methods.

Now, the difference between declaring a function "in-line" or setting it to a variable is that with this:

function Name() {
}

You can do:

var n1 = Name();
var n2 = Name();
var nn = Name(); // and so on...

But with:

var n = function Name() {
}

Is that n() will work and Name() will not. Even var a = Name() will throw an exception.

Here's a good article about this subject that worth a read. I hope it can help.

You can declare an object like you done in your second question, it's valid because a function is an object too. Or other ways like:

var Name = {
    render: function() {
    }
}

Name.render();

Or with prototype:

function Name() {
}

Name.prototype.render = function() {
}
// or
Name.prototype = {
    getByID: function() {
    },
    setName: function() {
    }
}
var n = new Name();

All these snipets are a valid object declaration.

Your second question may answer the first ones. When you declare a function like this:

function Name() {
    function render() {
    }
}
var n = new Name();

It is like render() be a private method. if you call outside the function name n.render(), you will see an error thrown because render is not recognized. But if you change to this...

function Name() {
    this.render = function() {
    }
}

... then n.render() will work like render() being a public method.

Now, the difference between declaring a function "in-line" or setting it to a variable is that with this:

function Name() {
}

You can do:

var n1 = Name();
var n2 = Name();
var nn = Name(); // and so on...

But with:

var n = function Name() {
}

Is that n() will work and Name() will not. Even var a = Name() will throw an exception.

I hope it can help.

You can declare an object like you done in your second question, it's valid because a function is an object too. Or other ways like:

var Name = {
    render: function() {
    }
}

Name.render();

Or with prototype:

function Name() {
}

Name.prototype.render = function() {
}
// or
Name.prototype = {
    getByID: function() {
    },
    setName: function() {
    }
}
var n = new Name();

All these snipets are a valid object declaration.

Your second question may answer the first ones. When you declare a function like this:

function Name() {
    function render() {
    }
}
var n = new Name();

It is like render() be a private method. if you call outside the function name n.render(), you will see an error thrown because render is not recognized. But if you change to this...

function Name() {
    this.render = function() {
    }
}

... then n.render() will work like render() being a public method. See this and this for further information about public and private methods.

Now, the difference between declaring a function "in-line" or setting it to a variable is that with this:

function Name() {
}

You can do:

var n1 = Name();
var n2 = Name();
var nn = Name(); // and so on...

But with:

var n = function Name() {
}

Is that n() will work and Name() will not. Even var a = Name() will throw an exception.

Here's a good article about this subject that worth a read. I hope it can help.

Source Link
DontVoteMeDown
  • 21.5k
  • 10
  • 72
  • 113

You can declare an object like you done in your second question, it's valid because a function is an object too. Or other ways like:

var Name = {
    render: function() {
    }
}

Name.render();

Or with prototype:

function Name() {
}

Name.prototype.render = function() {
}
// or
Name.prototype = {
    getByID: function() {
    },
    setName: function() {
    }
}
var n = new Name();

All these snipets are a valid object declaration.

Your second question may answer the first ones. When you declare a function like this:

function Name() {
    function render() {
    }
}
var n = new Name();

It is like render() be a private method. if you call outside the function name n.render(), you will see an error thrown because render is not recognized. But if you change to this...

function Name() {
    this.render = function() {
    }
}

... then n.render() will work like render() being a public method.

Now, the difference between declaring a function "in-line" or setting it to a variable is that with this:

function Name() {
}

You can do:

var n1 = Name();
var n2 = Name();
var nn = Name(); // and so on...

But with:

var n = function Name() {
}

Is that n() will work and Name() will not. Even var a = Name() will throw an exception.

I hope it can help.