Protect functions and var names on a javascript code -
im designing api requires users download javascript file server , load on pages. inside file there function call generic(), if users include js , reason have piece of js on page there function call generic() represent issue. im not front end dev, know php can solve creating class , putting functions inside, can call them $myclass->myfunction();, how can solve on js? approach on js? (no jquery please.)
you have expose @ least 1 identifier globally, common approach wrap in immediately-invoked function expression:
var yournamespace = (function () { var privatedata = 10; // not accessible outside iife // expose public properties (these functions can access private data) return { somemethod: function () { // stuff }, anothermethod: function () { // more stuff } }; }()); this expose single identifier, yournamespace, object properties can used methods. can use this:
yournamespace.somemethod();
Comments
Post a Comment