3

How can I call the function inside of this function?

var video = function() {

    this.name = "Name of Video";
    this.desc = "Short Description of Video";
    this.long = "Long Description of Video";

    function metadata(){
        return {
            name : this.name,
            shortDescription : this.desc,
            longDescription : this.long
        };
    };

};
Ben
  • 2,122
  • 2
  • 28
  • 48
  • 1
    Looking at the votes and answers would suggest that many people are unaware what prototype is in JavaScript. Travis is the only one who mentions this. Also a constructor function should be capitalized as to indicate you should invoke it with new. More about prototype: http://stackoverflow.com/a/16063711/1641941 – HMR Apr 09 '14 at 02:12

4 Answers4

8

Make it a method of the new object:

var video = function() {

    this.name = "Name of Video";
    this.desc = "Short Description of Video";
    this.long = "Long Description of Video";

    this.metadata = function(){
        return {
            name : this.name,
            shortDescription : this.desc,
            longDescription : this.long
        };
    };
};

var videoObject = new video();
videoObject.metadata();
Igor
  • 15,833
  • 1
  • 27
  • 32
5

You can't, other than within said function.

var video = function() {

    this.name = "Name of Video";
    this.desc = "Short Description of Video";
    this.long = "Long Description of Video";

    function metadata(){
        return {
            name : this.name,
            shortDescription : this.desc,
            longDescription : this.long
        };
    };
    metadata();

};
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • 1
    I want to be able to execute it from outside the main class/function any suggestions? – Ben Apr 08 '14 at 20:54
  • Sure, add it as a property of something that IS accessible outside. (other answers already demonstrate this) – Kevin B Apr 08 '14 at 20:57
4

jsFiddle Demo

There are several options. A highly used approach is prototypes. A prototype will extend the object created with the functions defined on the prototype if the new keyword is used. You can take advantage of this to expose functions.

var video = function() {
 if( !(this instanceof video) ){//ensure that we always work with an instance of video
  return new video();   
 }

 this.name = "Name of Video";
 this.desc = "Short Description of Video";
 this.long = "Long Description of Video";
};
video.prototype.metadata = function(){
 return {
    name : this.name,
    shortDescription : this.desc,
    longDescription : this.long
 };
};

Now the options, it can be called directly:

console.log(video().metadata());

It can be used as a function call and then referenced

var v = video();
console.log(v.metadata());

Or it can be explicitly instantiated and then referenced

var vid = new video();
console.log(vid.metadata());

This ensures that basically all uses of the function end up with the same functionality.

Travis J
  • 81,153
  • 41
  • 202
  • 273
1

You can't reach a nested function from the outside of the first outer wrapping-function directly:

See more info about that here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope

Therefore, an easy solution would be to use a function expression, attached to the returned object.

var video = function() {

    this.name = "Name of Video";
    this.desc = "Short Description of Video";
    this.long = "Long Description of Video";

    this.metadata = function(){
        return {
            name : this.name,
            shortDescription : this.desc,
            longDescription : this.long
        };
    };
};

new video().metadata();
Mik378
  • 21,881
  • 15
  • 82
  • 180