object oriented js

Object oriented javascript : 1.every function is object . class is defined using function as constructure 2.scope is function wise and other object oriented have block level scope 3. window is the root element in javascript. 4.function(){ console.log('ratan'); }(); // self executing function. and (window) can be passed insted of () i.e. passing parametre to anonymous function. 5.car.prototype.color = red. car.prototype.milage = function(distance,liters) { return distance/liters; } 6.prototype is implicit property. i.e not in object means var etc. when the prototype object is called it serch inside the object and then in prototype. 7.creating static variable - 8. car.model = sport; 9. object has key: value pair. 10. function has var var-name: value this.func_name = function() {}; 11. though window is a root . window is the global object. 12 . type this in chrom debug we get window as result 13. this === window => true . 14. javascript premitives : undefined : undefined. Null : null Boolean : true String: "foo" Number: 3.14 Object {bar :"baz"} --------- special objects : Function array regular expr 15. object is key-val pair 16. Premitives are pass by value , and object is pass by refrence so number1 = 100 and number2 = number1. number2= 300 assignment does not chnage the number1 value . -> var obj1 = {a:"ratan" , b:"kadam"}; var obj2 = obj1; obj2.a= "milind" ==> this change the value of the Obj1 as well as this is pass by refrence. 17. how to delete the property in object ? delete obj1.a; obj1.b= undefined --> does not delete the property it changes the value to undefined. 18. when a function is defined inside the object that is know as property. 19. This depends on how the function/ propery is called. imp by whome. sets the context. function mymeth1() { return this.a; // observe this keyword. } obj1.get = mymeth1(); // assigning property ro obj1. obj2.get = mymeth1(); ' --> obj1 will look like --> obj1 ={ a: "ratan" , b :"kadam" ,get:mymeth1}; obj2.get -- this will be from ob2.. depends on which object is calling. mymeth1.call(obj1) // passing the refrence. 20. var parent = { a: function fn(){return 1} , b:43 }; var child = Object.create(parent); child.b = 43; if any var is not available in the child/ grand child then it search the var/ function in parent. even obj.a is not available then this.a is searched in the parent. 21. Javascript has only prototypic inheritance. polyformism is easy .. just redefine the function in each object and calling obj-name.function() will call the function in that object. 22. all inheritance properties are defined in prototype. var answer = { get :function fn1() { return this.val }, val:42 }; var firmanswer = object.create(answer); // firm answer is object of answer. firmanser.val=3.14; firmanswer.get = function fn2() { return answer.get(); }; firmanswer.get() // will call the answer.get and the context is of answer as the firmanswer.get calls the answer.get() so the val is picked from the answer. to pick the val var variable from firm answer we need to pass the context - this ( this belong to firmanswer) then answer.get() the function is executed from answer but this is paased from firmware anser.get.call(this) so that the val is searched in firmware as this refers to the firmware . https://www.youtube.com/watch?v=PMfcsYzj-9M

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.