Avoid conflicts with function naming conventions javascript - Stack Overflow

I have a bunch of functions in my script which resides in a .js file.How can avoid conflicts with the

I have a bunch of functions in my script which resides in a .js file. How can avoid conflicts with the names of my functions within the same page if some other script written by some other guys use the same function names as in my script ?

Is there a way to do this?

I have a bunch of functions in my script which resides in a .js file. How can avoid conflicts with the names of my functions within the same page if some other script written by some other guys use the same function names as in my script ?

Is there a way to do this?

Share Improve this question asked Apr 9, 2013 at 10:03 Claudio FerraroClaudio Ferraro 4,7316 gold badges49 silver badges83 bronze badges 1
  • u cannot avoid it pletely, but if u wrap in inside a class that will much of sense – ManMohan Vyas Commented Apr 9, 2013 at 10:05
Add a ment  | 

2 Answers 2

Reset to default 7

If you don't need access to those functions outside of your script you can wrap the whole script in an immediately invoked function expression:

(function () {
    // Your code here
}());

This introduces a new scope, so any declarations within it are not visible outside of it.

If you do need access outside of that scope, expose your functions as methods of a "namespace":

var YourStuff = (function () {
    // Private functions etc...

    // Expose public methods
    return {
        someMethod: function () {}
    };
}());

By taking this approach you only introduce a single global identifier, reducing the chances of a conflict. You can call the method as follows:

YourStuff.someMethod();

Use namespaces..

var pany = {};

pany.doSomething = function() {
};

pany.project = {};
pany.project.submodule = {};
pany.project.submodule.doSomething = function() {};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745651757a4638305.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信