title; this.price = price; console.log(this); // newly created object return 9; } let p1 = new Product('Pen', 1000); // called as a constructor console.log(p1); // newly created object
apply, call, or bind method ✓ Value of ‘this’ is manually passed as first parameter of apply and call ✓ New object is created and passed as value of ‘this’ for the bind
owner: 'foo', price: 1000 } function Product(title, price) { this.title = title; this.price = price; console.log(this); // Car object with passed title and price properties return 9; } let p2 = Product.apply(Car, ['Pen', 1000]); // Indirectly called using apply let p1 = Product.call(Car, 'Pencil', 700); // Indirectly called using call console.log(p1); // 9 console.log(Car); // only one Car object
inside a function depends on how a function is invoked But there is a catch , However, it also depends on the type of the function or how the function is created
JavaScript by Kyle Simpson Book – JavaScript for Impatient Programmer by Dr Axel Rauschmayer Blog - https://debugmode.net/category/javascript-2/ [email protected]