2

can you please tell me how to do encapsulation in javascript .I have a class Name Car .I want to extend this class with B class .Secondly I want to override and overload the methods in java script.

Here is my fiddle http://jsfiddle.net/naveennsit/fJGrA/

   //Define the Car class
function Car() { }
Car.prototype.speed= 'Car Speed';
Car.prototype.setSpeed = function(speed) {
    this.speed = speed;
    alert("Car speed changed");
}


//Define the Ferrari class
function Ferrari() { }
Ferrari.prototype = new Car();


// correct the constructor pointer because it points to Car
Ferrari.prototype.constructor = Ferrari;

// replace the setSpeed method
Ferrari.prototype.setSpeed = function(speed) {
    this.speed = speed;
    alert("Ferrari speed changed");
}

var car = new Ferrari();
car.setSpeed();

can you explain these two lines

Ferrari.prototype = new Car(); This line show Ferrari is extend by car ?

 Ferrari.prototype.constructor = Ferrari;

what is the used of this line ?

2 Answers2

0

JS, by design does not provide a built-in way to manage the visibility of members of an object. but its flexible enough to allow us to do encapsulation.

Effective write up i found for you is http://www.codeproject.com/Articles/108786/Encapsulation-in-JavaScript

Waqas Memon
  • 1,247
  • 9
  • 22
  • I already found this link ,Actually I am begineer so I need to go step by step can you change my fiddle .. extend by class by B –  Sep 16 '13 at 08:54
  • can you explain these two lines **Ferrari.prototype = new Car();** This line show Ferrari is extend by car ? Ferrari.prototype.constructor = Ferrari; what is the used of this line ? –  Sep 16 '13 at 09:02
0
Ferrari.prototype = new Car()

This method adds Car poperties to the Ferrari's prototype. Whatever is returned by the Car(), is added to the existing Ferrari's prototype.

Ferrari.prototype.constructor = Ferrari

Prototype has a constructor property which is overriden by this call Ferrari.prototype = new Car(). This is manually resetting it again.

prototype and constructor object properties

I have edited your fiddle. http://jsfiddle.net/fJGrA/3/

Using closure in javascript you can hide elements within an object or function.

function foo(args){
//this is a private variable.
 var _name = ""

 return{
  getname:function(){
   return _name
   }
  }

 }

 bar = new foo()

 // name can only be accessed from inside the foo function.

Whenever a variable is created with a var keyword within a function, it is accessible only in the scope of that function. Effectively, being private to it.

Community
  • 1
  • 1
Selvam Palanimalai
  • 1,550
  • 1
  • 10
  • 13
  • That is just a normal variable that kinda looks like it's working in toy examples, it's actually an anti-pattern. – Esailija Sep 16 '13 at 09:07
  • @Esailija: What are the other ways to emulate private variables in javaScript? – Selvam Palanimalai Sep 16 '13 at 09:10
  • If you want to emulate private fields, then you should realize that private fields in any other language are accessible through reflection and for important reasons. You are making it impossible to access them in any way, even for other internal classes. You are also not using the object's fields but normal variables. You can use underscore prefixing for much better emulation because that uses the object's fields and allows deliberate access. – Esailija Sep 16 '13 at 09:12
  • Object fields are accessible by anyone who has reference to the object. And they can edit it at free will. My example is a simple implementation of modular pattern. And the only way to access these variables are through getter/setter methods. Scroll down to the section of modular pattern.http://addyosmani.com/resources/essentialjsdesignpatterns/book/. – Selvam Palanimalai Sep 16 '13 at 09:15
  • Look at crockford's concept of private variables. He does it through closure too! http://javascript.crockford.com/private.html – Selvam Palanimalai Sep 16 '13 at 09:17
  • If you mean Module pattern then that is not related, the OP was asking about classes. – Esailija Sep 16 '13 at 09:17
  • Even if that page wasn't written 10 years ago, mind you that advice is from someone who writes code like this https://github.com/douglascrockford/JSLint/blob/master/jslint.js#L1497 – Esailija Sep 16 '13 at 09:19
  • Found something relevant . http://people.apache.org/~martinc/OOP_with_ECMAScript/ – Selvam Palanimalai Sep 16 '13 at 09:29
  • Sorry but anyone doing that doesn't understand [the point](http://martinfowler.com/bliki/AccessModifier.html#AccessControlDoesNotControlAccess) in the first place. Giving up inheritance, polymorphishm, instanceof, generic methods and so on just to get back an illusion of security is ridiculous. – Esailija Sep 16 '13 at 09:33
  • True. But then blame initial ECMAscript ! JavaScript language was never meant to do class based inheritance. ECMAscript v6 is making amends now in that area. – Selvam Palanimalai Sep 16 '13 at 09:36
  • It has nothing to do with class-based inheritance. For example Python has classes and it uses underscore prefix too. It simply makes the language simpler due to not having to implement such heavy weight features which are not needed. – Esailija Sep 16 '13 at 09:39