RcBuilder@walla.com
call centre: 054-5614020

OOP in javascript

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / OOP / prototype   /   No Comments
26
Jan
2016

custom Objects -------------- // Literal (a single instance) var person = { firstName: 'Roby', lastName: 'Cohen', sayYourName: function () { return this.firstName + ' ' + this.lastName; } }; console.log(person.sayYourName()); // Roby Cohen ---- // Factory (multiple instances) function PersonFactory(firstName, lastName) { return { firstName: firstName, lastName: lastName, sayYourName: function () { return this.firstName + ' ' + this.lastName; } }; } var person = PersonFactory('Roby', 'Cohen'); var person2 = PersonFactory('Avi', 'Cohen'); console.log(person.sayYourName()); // Roby Cohen console.log(person2.sayYourName()); // Avi Cohen ---- //.

Read more

javascript fixing the ‘new’ keyword

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / prototype   /   No Comments
26
Jan
2016

javascript fixing the lack of 'new' keyword when creating an instance of an object when creating an instance of the object and forget to use the 'new' keyword the 'this' variable will refer to the 'window' object. we can use this behavior to initialize a fresh instance of the current object and return it. this way we'll be able.

Read more

javascript instanceof

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / prototype   /   No Comments
26
Jan
2016

javascript instanceof structure: instanceof e.g: person instanceof Person using: function Person(Name) { this.Name = Name; } function Employer(Name) { this.Name = Name; } var person = new Person('Roby Cohen'); var employer = new Employer('Roby Cohen'); console.log(person instanceof Person); // true console.log(employer instanceof Person); // false console.log(employer instanceof Employer); // true if (person instanceof Person) console.log('PERSON : ' + person.Name); // PERSON : Roby Cohen notice! any class inherit from object therefore.

Read more

javascript private/public

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / OOP / prototype   /   No Comments
26
Jan
2016

javascript private/public data members: private access defined using var within the object, can't be accessed from outside the object example: function Person(firstName, lastName) { var firstName = firstName; // data member var lastName = lastName; // data member ... ... } properties: public access defined using this, can be accessed from outside example: function Person(firstName, lastName) { this.firstName = firstName; // public property this.lastName = lastName; // public property ... ... } notice! we can define properties.

Read more

javascript that

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / OOP / prototype   /   No Comments
26
Jan
2016

javascript that use local variable within a custom object to save reference to the current object sometimes the 'this' variable not refer to the object contains the current function (callback calls, setTimeout etc.) the best way to handle this is by save an extra reference to the actual object (it's quite common to call it 'that') declare it in.

Read more

javascript override

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / OOP / prototype   /   No Comments
26
Jan
2016

javascript override function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } Person.prototype.sayYourName = function () { console.log(this.firstName + ' ' + this.lastName); } // override 'toString' Person.prototype.toString = function () { return 'My Name is ' + this.firstName + ' ' + this.lastName }; var person = new Person('Roby', 'Cohen'); // override sayYourName method (change the reference) // only for the current instance person.sayYourName = function.

Read more

javascript inheritance

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / prototype   /   No Comments
26
Jan
2016

javascript inheritance using prototype - best practice: (see 'prototype' above) using reference to the base class example: function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; this.sayYourName = function () { console.log(this.firstName + ' ' + this.lastName); } } function Employee(firstName, lastName, salary) { var parent = new Person(firstName, lastName); // inheritance by reference var salary = salary; // data member this.sayYourSalary = function() { console.log(salary); } this.sayYourName = function() { parent.sayYourName(); console.log('i make.

Read more

javascript prototype

Posted by: RcBuilder  /   Category: class / function / Object / OOP / prototype   /   No Comments
26
Jan
2016

javascript prototype * use prototype to save CPU usage and Memory consumption by defining the object functions directly on it - e.g - function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } // this 'sayYourName' method will create only once for all Person instances // instead of per each instance - less CPU usage (no need to create it per instance) //.

Read more

javascript object using prototype

Posted by: RcBuilder  /   Category: class / function / JAVASCRIPT / Object / OOP / prototype   /   No Comments
26
Jan
2016

javascript object using prototype // prototype (best practice) function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } // this 'sayYourName' method will be created only once for all Person instances // instead of per each instance - less CPU usage (no need to create it per instance) // and less Memory consumption (no need to store it per instance) // it's recommended.

Read more
  • פיתוח מערכות
  • פתרונות טכנולוגים
  • קידום אתרים
  • בניית אתרים