最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I call a function inside of a function? - Stack Overflow

programmeradmin1浏览0评论

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

};

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

};
Share Improve this question asked Apr 8, 2014 at 20:52 BenBen 2,2693 gold badges30 silver badges49 bronze badges 1
  • 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: stackoverflow./a/16063711/1641941 – HMR Commented Apr 9, 2014 at 2:12
Add a ment  | 

4 Answers 4

Reset to default 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();

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

};

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.

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/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();
发布评论

评论列表(0)

  1. 暂无评论