开发者

Using a javascript library (underscore.js) within a class of my own

I'm trying to call a method from the underscore.js library within a class of my own. Something like this:

document.write('<scr'+开发者_如何转开发'ipt type="text/javascript" src="Helpers/underscore-min.js" ></scr'+'ipt>');
function MyObject(object){
    this.object = object;

    this.RandomMethod = function(object)
    {
        var result = _.isEqual(object, this);
    }
}

I am able to use it if I do it outside of the object declaration, but if it try to access it like this, the object does not exist.

Can somebody help?


The script you are adding has not had time to load yet since scripts are loaded asynchronously. To test this, try adding a timeout to delay the script execution:

document.write('<scr'+'ipt type="text/javascript" src="Helpers/underscore-min.js" ></scr'+'ipt>');
setTimeout(function() {
    //your code here
}, 2000); //delay for 2 seconds to give script time to laod

This isn't a solution, just a way to verify the problem. If this is what's going on check out this article:

4 ways to dynamically load external JavaScript(with source)

You may want to employ the fourth option. It allows you to determine when the script has finished loading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜