Question: I can see how you manage the global name "calculator" from being overwritten but how do you protect your methods like "add" from being overwritten as well? Should the same technique be used (calculator.add = calculator.add || ....)?
in a real world example those functions would probably still depend on properties and other functions of the calculator object so it won't help just creating a new empty object. Imagine if we had functions or properties on the calculator object that all other functions in calculator share in common. Its good to always keep track on your modules so you know when they are available to you aswell as trying to reduce their dependencies on eachother so that they can be exported to other projects without breaking for example. mod.counter = { callcount: 0, add: function(){ this.callcount++; }, reset: function(){ this.callcount = 0; } }; modules like this depends on the initialization of the module so we cannot extend it until we have initialized it. And as for the collision risk with global variables you dont always need selfexecuting functions you can build your whole application upon just one global object and be just fine but if you want variables or functions to be kept private i highly recommend this approach: var counter = (function(){ // PRIVATE var callcount = 0; // EXPOSED return { add: function() { callcount++; }, reset: function() { callcount = 0; }, getCount: function() { return callcount; } }; }());
watching this in 2019 :D
I have to pile on in regards to the sentiment in the comments...
Adroit presentation!
Wow! its still super useful in 2020 :) Thanks for sharing
You're a great teacher. Keep up the good work. Fantastic video
Excellent tutorial and very easy to follow.
Great teaching style. Well done.
Thanks, Jason!
Thanks, Antonio.
You're the first to use "adroit". :)
Cary
Thanks, Tony!
Since we are assigning the "add" variable instead of amending it, we are safe.
If we were amending to the "add" namespace, we would use the || syntax.
Nice to know about AMD. Thanks for the video
Cheers
Question: I can see how you manage the global name "calculator" from being overwritten but how do you protect your methods like "add" from being overwritten as well? Should the same technique be used (calculator.add = calculator.add || ....)?
Great video , Thanks
terrific
very nice.
thank you so much! 謝謝你!
very very very good work!
Thanks!
Cheers!
Thank you
Ahh, thanks. Nice thing to say.
Cool - thanks!
Thanks Dude
Hypothetically, Isn't possible that another script is also assigning the 'add' variable?
Wish you were my prof
Ugly code editor jaja... but excellent explanation! Thanks for the tutorial. I'm going to watch part 2 :)
lol - that was four years ago. Editor is much better now. :)
yeah I knew it was an unfair comment... jaja
It is possible.
in a real world example those functions would probably still depend on properties and other functions of the calculator object so it won't help just creating a new empty object. Imagine if we had functions or properties on the calculator object that all other functions in calculator share in common. Its good to always keep track on your modules so you know when they are available to you aswell as trying to reduce their dependencies on eachother so that they can be exported to other projects without breaking for example.
mod.counter = {
callcount: 0,
add: function(){
this.callcount++;
},
reset: function(){
this.callcount = 0;
}
};
modules like this depends on the initialization of the module so we cannot extend it until we have initialized it. And as for the collision risk with global variables you dont always need selfexecuting functions you can build your whole application upon just one global object and be just fine but if you want variables or functions to be kept private i highly recommend this approach:
var counter = (function(){
// PRIVATE
var callcount = 0;
// EXPOSED
return {
add: function()
{
callcount++;
},
reset: function()
{
callcount = 0;
},
getCount: function()
{
return callcount;
}
};
}());
The example was arbitrary to lead up to describing the AMD pattern.
Why, it's almost like Cary has a better solution than the one in this video. As if! :)
Link to the second part (2): ruclips.net/video/HwO_qwcJ4rs/видео.html
Thanks!
Thanks!