Note the shorthand instead of "{add: add, substract: substract, print: print};" you can now write return {add, substract, print}; And thanks for this clear demonstration.
I have never found most precise, concise and trick catching instructive series on RU-vid or Udemy like this, really good baked pastry been brought only by "bakers". If you would author a book on JS it will be of most useful one
it seems that the returned object is nothing but bunch of functions,these bunch of functions share the out parameter i,they can modify this i and return the object itself,which was cool, thanks sith!
Is it possible to chain methods from factory functions? E.g. sayName().location().favFoods(). I can't use the `return this` keyword inside the factory function like how you would with a regular constructor function :( const nerd = (user, city, food) => { const { sayName, location } = Person(user, city) const { favFoods } = FaveFood(food, user) // console.log(`${user} likes nerd stuff!`) return { sayName, location, favFoods, doSOmethingNerdy } return this }
one question, please is it correct to do the following code? var obj = function () { var oper = { i: 0, add: function (j) { this.i += j return this }, sub: function (j) { this.i -= j return this }, print: function () { console.log(this.i) }, } return oper } var x = obj() //no need new x.add(3).sub(2).print() //1 console.log(x) I hope your answer, thank you. Blessings
hi, how to limit method chain to some perticular function ? returning this will give all method access, and i am trying to make a routing package where i need to return a chain of method like router.get() here i only want to return get, post and other method but i have many methods in class that all are showing up, i also have applyMiddleware() method that i only want after method is selected but it is showing while accessing router.* anything
+Sheshagiri Pai Thanks for watching the video . Following is the explanation you asked. var obj = function() { this.i = 1; this.geti = function(){ return this.i;}; }; the above behaves more like a class. so if i need to create an object from this class i can use 'new' like this var a = new obj(); var b = new obj(); var c = new obj(); as you can see i can create multiple object from this obj class function . Basically 'new' here means creating a new object. Now lets look at closure var obj = function() { var i = 1; var geti = function(){ return i;); return {geti:geti}; }; var a = obj(); here i when i do var a= obj(); , i am not creating a new object . obj() is actually returning me a object with function geti . you can see in this statement "return {geti:geti};" that is why i am not using new. also in the first example you dont see return statement. Thanks
Hi, I liked your tutorial. Is there other way than just return{add:add, substract:st...} and why didn't you use new obj()??? Could you please guide me :)
when you run the function it's returning the object with mehtods with closures. which means this is not a function constructor. only function constructor uses new. I have a tutorial on that called "constructor" you can look at that. You can return like this jsfiddle.net/7dg1g3j8/ does that answer your question?
Your quick response is highly appreciated. I really liked your example at jsfiddle.net/7dg1g3j8/ and something like this "const cal = () => {" is very new to me. It was a great learning. Thank you for sharing your knowledge. Have a great time ahead and keep publishing :)
Nice explanation sir.. I have one doubt is that what is difference between creating object with new and without new operator because which i know (if i am wrong then rectify me) that while creating an object with new operator they maintain there own data member & method then i tied with above example without using new operator i was thinking that may be they are referring to same address/reference Without Using new Operator var x = obj(); var y = obj(); console.log(x===y) // false x.add(3).substract(2).substract(2).print(); // -1 y.add(3).substract(2).print(); // 1 With Using new Operator var x = new obj(); var y = new obj(); console.log(x===y) // false x.add(3).substract(2).substract(2).print(); // -1 y.add(3).substract(2).print(); // 1 can you help me to clear my this doubt..Thanks
What you are looking for is understanding of how to create objects diffrent ways. Here is one of my tutorial that explains that. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-xizFJHKHdHw.html
new is a constructor pattern. Basically when you want to create objects from a constructor. If you are doing functional programming where you are returning functions with closures and such you are using with out new. You should watch my object oriented series to understand 'new' keyward . ru-vid.com/group/PL7pEw9n3GkoW0ceMeoycg9D00YjPAbtvt
This is a topic that's been on my mind for some time now, I've read blogs about method chaining but never quite understand it. Watching this video now makes me understand it better. Thanks
When you return this. you return refrence to yourself ( in this case the main function ) , this way now you can can call the methods defined in itself using dot notation . x.add().add();
but in the 'add' method for example, returning 'this' there will return the main function itself, but I don't understand why it doesn't return the add function
In this case you only have one function. what if you have many functions like "add" you dont want to return only one function . instead you should return "this" so you can chain any functions.