I was going to sleep but then I came across a video of yours and binge-watched all the videos in the Tutorials playlist. And it is 3.30AM. So, awesome work!!
Overall good video, though I would just say be careful when doing Employee.prototype = { getFoo: function () { ... code ....}}; as this will overwrite the original prototype reference! It’s safer to do Employee.prototype.getFoo = function () {.....};
Is there a particular reason that you always do JavaScript OOP using the prototype method vs class kw? I like what you are doing -continued Success 👍🏾💯👌🏾
Hey, greeting from Bulgaria. Thanks for making this videos. Your explaining stuff really good. My request is. Can you make videos for the PubSup and Mediator patters. ^^
Nice tutorial. I’m trying to think about a practical use case for this. Is this scenario valid.. The Employee class is manipulated by the HR department who hires employees, logs their information and such. The ExtraSalary class is handled by the Finance department?
What is the benefit of this over simply writing: devsage.salary = devsage.salary * 2; Right? We just changed the internal state of an object instance of a constructor function. What did we achieve? I'm sure there is a point to it, and it is useful in some situations. However, I didn't understand why we did it.
Suppose we have a method which calculates monthly salary of employees. Now company has decided to add spot bonus for few employees. In that case we can create a modifiedSalary() function. This function will contain a variable 'spotBonus' which gets added to calculatedSalary of employees. In this way we don't need to modify existing class/ function.
var DevSage = new Employee('DevSage',10000); console.log(DevSage.getSalary()); // 1000 function doubleSalary(DevSage){ DevSage.setSalary(DevSage.getSalary() *2) ; } doubleSalary(DevSage); console.log(DevSage.getSalary()); //2000 We can also call this way, Can you please telll me why do we need accept method here ?
I suppose either way could work. I don't see any major advantage. The Visitor pattern is not very widely used/known in Javascript but in other languages there may be good reasons to use it.
@@mfhsieh6858 well, I meant that they are obsolete in a way that they aren't that interesting to a programmer any more, and JS can do some optimizations on classes because it knows what you are doing
🤖 GitHub: github.com/pkellz/devsage/blob/master/DesignPatterns/Visitor.js
📕"Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ
💙 Twitter: twitter.com/realDevSage
📙 Ebooks: payhip.com/devsage
💥 Discord: discord.gg/BP8wPv6raA
I was going to sleep but then I came across a video of yours and binge-watched all the videos in the Tutorials playlist. And it is 3.30AM.
So, awesome work!!
Glad you like them!
2 years later and I'm watching this at 0355am
I happy that I "visited" DevSage!
I've been struggling to understand this, thanks a lot!
You're welcome!
Amazing videos. Very nice explanations. Would like you to consider making a video on composite pattern
Overall good video, though I would just say be careful when doing Employee.prototype = { getFoo: function () { ... code ....}}; as this will overwrite the original prototype reference! It’s safer to do Employee.prototype.getFoo = function () {.....};
That's actually a really good point. I hadn't even thought about that until reading this comment. Thanks
very cool, thanks for shering, exellent channel!!!
I went to watch this video and realized I'm watching it at the exact same time it was recorded 2 years later. 3:28 PM on December 7th
😅😅
Maybe it's some kind of omen
very good series
Is there a particular reason that you always do JavaScript OOP using the prototype method vs class kw? I like what you are doing -continued Success 👍🏾💯👌🏾
Thanks!
Hey, greeting from Bulgaria. Thanks for making this videos. Your explaining stuff really good.
My request is. Can you make videos for the PubSup and Mediator patters. ^^
Greetings. Thank you for the compliment. I have a video about the mediator pattern here -> ua-cam.com/video/ZuhgOu-DGA4/v-deo.html
+1 from Bulgaria! :D
nice pattern! thanks!
No problem!
isnt this is same as bind?
Nice tutorial. I’m trying to think about a practical use case for this. Is this scenario valid.. The Employee class is manipulated by the HR department who hires employees, logs their information and such. The ExtraSalary class is handled by the Finance department?
Yeah that actually sounds like it could be a valid application
Hey man great video but do you think solving this with class that came with ES6 is simpler.
just a bit simpler :o
class Employee{
constructor(name, salary){
this.name = name;
this.salary = salary;
}
getSalary(){
return this.salary;
}
setSalary(salary){
this.salary = salary;
}
accept(visitorFunction){
// this is a reference to our Employee
visitorFunction(this);
}
}
const devChris = new Employee("Chris", 10000);
console.log(devChris.getSalary());
function ExtraSalary(emp){
emp.setSalary(emp.getSalary() * 2);
}
devChris.accept(ExtraSalary);
console.log(devChris.getSalary());
@@CrassCriss thanks dude, though now I have become very much comfortable with js from the time the comment was posted
@@karthickdurai2157 haha okay, just saw the video today sorry... well..maybe its useful for someone else :)
Thank you
What is the benefit of this over simply writing:
devsage.salary = devsage.salary * 2;
Right? We just changed the internal state of an object instance of a constructor function. What did we achieve?
I'm sure there is a point to it, and it is useful in some situations.
However, I didn't understand why we did it.
Useful for AST walking
Thank you very much. Now it is very clear how this pattern works, but I am still a bit confused about its use case.
Suppose we have a method which calculates monthly salary of employees. Now company has decided to add spot bonus for few employees. In that case we can create a modifiedSalary() function.
This function will contain a variable 'spotBonus' which gets added to calculatedSalary of employees.
In this way we don't need to modify existing class/ function.
var DevSage = new Employee('DevSage',10000);
console.log(DevSage.getSalary()); // 1000
function doubleSalary(DevSage){
DevSage.setSalary(DevSage.getSalary() *2) ;
}
doubleSalary(DevSage);
console.log(DevSage.getSalary()); //2000
We can also call this way,
Can you please telll me why do we need accept method here ?
I suppose either way could work. I don't see any major advantage. The Visitor pattern is not very widely used/known in Javascript but in other languages there may be good reasons to use it.
@@DevSage Thank you
How do you feel about making a video on javascript prototypes?
Hmm.. I will definitely consider it!
I think they are obsolete, considering that classes are introduced, and if you worry about compatibility, there's transpilers
@@ishdx9374 I don't think so. ES6 class is sugar syntax, it main concept is from prototype inheritance. So, it’s good to know the prototype.
@@mfhsieh6858 well, I meant that they are obsolete in a way that they aren't that interesting to a programmer any more, and JS can do some optimizations on classes because it knows what you are doing
It does resembles decorator pattern.
lowkey thought this was a fireship video