-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the way how variables are declared #111
Comments
Hi @SergiSMJ So @marklonquist and i played around with it, for best practice and possibility for extending the objects, and we came up with this "new" syntax (see this fiddle): // Original file
var novicell = novicell || {};
novicell.overlay = new function () {
this.init = function() {
console.log('init');
}
};
// Customer extentions file
var novicell = novicell || {};
novicell.overlay.extensions = new function() {
this.test = function() {
console.log('again');
}
this.testtwo = function() {
console.log('hello');
}
}
// Master.js
var novicell = novicell || {};
novicell.overlay.init();
novicell.overlay.extensions.test();
novicell.overlay.extensions.testtwo(); What do you think about this? |
Hi @Dan9boi cool! I think this new syntax is better than the old and does not lead to confusion, is easy to made extensions and if are good is easy to implement in the package too :) |
Hi @Dan9boi , I'm a colleague of @SergiSMJ. I also like this syntax better, but I have a few comments:
At the beginning of the extending file we lazy init the "novicell" variable (all fine), but then we use "novicell.overlay". In the case this file get loaded first it will crash, since the "novicell" is just initialized and doesn't have an "overlay" property. The other thing I found is, in "Master.js", we also lazy init the "novicell" variable. We're using properties of the "novicell" variable, but in case the variable get initialized, the second line will crash (no "overlay" property). After changing the code: // Original file
var novicell = novicell || {};
novicell.overlay = novicell.overlay || {};
novicell.overlay.init = function() {
console.log('init');
};
// Customer extentions file
var novicell = novicell || {};
novicell.overlay = novicell.overlay || {};
novicell.overlay.extensions = new function() {
this.test = function() {
console.log('again');
}
this.testtwo = function() {
console.log('hello');
}
};
// Master.js
novicell.overlay.init();
novicell.overlay.extensions.test();
novicell.overlay.extensions.testtwo(); What do you thing about this changes? |
For me is more simple, this syntax with prototyping rocks but I think we don't need to use the "lazy init" var novicell = novicell || {};
novicell.overlay = novicell.overlay || {}; because in the package we compile with gulp the extensions in order: scripts: [
projectPath + "scripts/components/novicell.js",
projectPath + "scripts/components/novicell.debounce.js",
projectPath + "scripts/components/novicell.visible.js",
projectPath + "scripts/components/novicell.embed.js",
projectPath + "scripts/components/novicell.overlay.js",
projectPath + "scripts/components/novicell.cookieinfo.js",
projectPath + "scripts/components/novicell.map.js",
projectPath + "scripts/components/novicell.font.js",
projectPath + "scripts/master.js"
] Use lazy init with this configuration I think is pointless, if we use "Lazy init" maybe we can change the way to compile the javascript extensions in gulp on the config.js to this way: scripts: [
projectPath + "scripts/components/*.js",
projectPath + "scripts/master.js"
] Because thanks to the lazy init I think we don't need to worry about the order :) |
@hakudoshi23 you are totally right. We should do the lazy init of the scripts: [
vendorPath + "novicell.overlay/components/novicell.js",
vendorPath + "novicell.debounce/components/novicell.debounce.js",
vendorPath + "novicell.map/components/novicell.map.js"... I'm going to make a new issue for changing the syntax on the existing scripts 💯 |
@SergiSMJ that's why we need the extention to Work 😎 |
the final syntax is going to be like this: https://jsfiddle.net/6r5d1z2g/4/ |
This is cool, and make sense to me, but after this, we declare the new var like this:
example:
I think is better, and make more sense to me declare the var like this:
The other way, for me, is a little bit "antipattern" and it does not make sense because this declaration is for avoiding "override" when you create a function in javascript, but this way to code must be used for the users that use the package, not by the package, maybe with a little part of code you can understand me:
You can test it on: https://jsfiddle.net/odp1do49/1/
If the user using the package create a function with the same name this will override the function even if it's written this way:
novicell.package = novicell.package || function() {}();
, and that's because for me not make sense write the function in this way on the package.Regards :)
The text was updated successfully, but these errors were encountered: