A way to get good JavaScript coverage

I'm not a very good JavaScript user, but I can handle it. I'm not proud of the code written in JavaScript, so I decided to change it. Here is my first step:

I am trying to create my own library for a project, and below is the initial structure.

window.fooLib = {}; (function (foo) { "use strict"; foo.doSomeStuff = function(param1) { console.log(new AccommProperty(param1)); } //some internal function function AccommProperty(nameValue) { var _self = this; _self.name = nameValue; } }(fooLib)); 

I immediately called a function expression to initialize my variable. In this case, it is fooLib .

I'm not sure if I have to do anything else to make window.fooLib more secure. I mean, this can be overridden by any other code that will work after my code if I understood JavaScript correctly.

What are your thoughts?

+7
source share
3 answers

If you want to prevent your variables from being overwritten, you can use Object.defineProperty() with write options: false, configurable: false. In your case:

 (function () { "use strict"; var foo = {}; //some internal function function AccommProperty(nameValue) { var _self = this; _self.name = nameValue; } foo.doSomeStuff = function(param1) { console.log(new AccommProperty(param1)); } Object.defineProperty(window, "foolib", {value:foo}); }()); 

However, there is no good reason for this. EcumScript 5.1 is required for operation, and there are no gaskets around it; maybe something with getters / setters to prevent overwriting with the = operator.

But it should also not be necessary to make your library unregistered. Just don't use the code on your site that overrides lib. Or maybe someone even wants to rewrite your functions with a different, better lib with the same interface?

If the question is about a library to be shared, with possible namespace conflicts with others, you can take a look at jQuery.noConflict .

+1
source

Each JavaScript object can be redefined. This is the nature of JavaScript and cannot be changed. Therefore, you cannot make the code safe in this sense.

As for self-inflating functions: you should use them if you want to have local variables, but accessible to all your functions. So in your case AccommProperty has such a variable. Defining the inner scope of doSomeStuff does not matter if doSomeStuff does not use variables defined within the scope.

Therefore, when you want to hide variables from the user and / or you need global variables, and you are afraid of name conflicts, use self-employed functions.

0
source

I'm not sure that I have to do some other things to make window.fooLib more secure. I mean, this can be overridden by any other code that will work after my code if I understood JavaScript correctly.

Instead, you can try making window.fooLib local variable. Using closures and nested functions , you can emulate a namespace where you can put all your data instead of putting it in a global scope or binding an object to a window :

 (function() { // all functions nested in foo() have access to fooLib. fooLib = {} fooLib.doSomeStuff = function(param1) { console.log(param1); console.log(fooLib); } //some internal function function AccommProperty() { console.log(fooLib); } }()); 

See Closing Javascript: Encapsulating Related Functions for more details.

-one
source

All Articles