Showing posts with label oop. Show all posts
Showing posts with label oop. Show all posts

Thursday, May 5, 2011

javascript object oriented programming 4 - private members

Let's check the code:
Car = {
   model : 'mazda 3',
   getModel : function() {return this.model},
}
//we can access the model property directly
console.log(Car.model);
//how about we use function constructor?
var Car = function() {
    this.model = 'mazda 3';
}
var newCar = new Car;
//we still can access the model property directly.
console.log(newCar.model);


In the above examples, we can't hide object's properties from being exposed. What if we want to have private properties and private methods/functions that can be accessed only in the object, just like what we do in java and php 5?

Same as we use object scope to achieve namespace, we can also create private members for our object.
var Car = function() {
    //the model variable is only visible within the Car object now!
   //so it becomes a private property of Car
    var model = 'mazda 3';
    //since function, which is object in js, can be assigned to a variable,
    //we can have private function too!
   // this upgrade function is only visible in Car scope!
    var upgrade = function(newModel) {
        model = newModel;
    };
    //a public method.
    this.getModel = function() {
        upgrade('mazda 6');
        return model;
    }
}
var newCar = new Car();
//will get undefined
console.log(newCar.model);
//will get error
newCar.upgrade('toyota');
//will get mazda 6
newCar.getModel();


Some people say javascript is a functional programming language, some argue it is object based language because almost everything is object in javascript, while others prove that javascript can have full OOP features just like other modern object oriented programming languages like java. 

I would say, javascript is a plasticine programming language! You can shape it as whatever you want !

Actually, i think we don't even need to bother about implementing those private member stuff. If we really want to have private, we can simply use naming convention: private members must be prefixed with underscore, for example:
var Car = function() {
    this._model = '3';
    this._upgrade = function(newModel) {
        this._model = newModel;
    };
    this.getModel = function() {
        this._upgrade('mazda 6');
        return this._model;
    }
}

And then, just tell your team members to follow this coding convention: when you see something prefixed with _, you know you shall not directly access it or change its status.

Saturday, March 26, 2011

javascript object oriented programming 1:create objects

how to create objects One way to create an object:
var ship = {
     wings: 'wings',
     body: 'body',
     jet: 'jet',
     guns: 'guns'
};
Now, we have a ship object. This object has 4 properties:wings,body,jet,guns. An object property can contain a function, we call it a method.
var ship = {
     wings: 'wings',
     body: 'body',
     jet: 'jet',
     guns: 'guns',
     fire = function () {
         return 'start to fire';
     }
};
Now our ship object has a method fire. Accessing Object's properties We can access an object's properties by using either square bracket notation or dot notation. For example: ship.wings or ship['wings']. Dot notation is easier to read and write. Accessing a non-existing property returns 'undefined': ship.tail. Calling Object's methods ship.fire() invokes the fire function and returns the value 'start to fire'. Calling a non-existing method causes error: ship.move(). Using keyword 'this' Let 's add a method to our ship.
var ship = {
     wings: 'wings',
     body: 'body',
     jet: 'jet',
     guns: 'guns',
     fire = function () {
         return 'start to fire';
     }
     getGuns = function() {
         return this.guns;
     }
};
'this' actually refers to this object or current object. Another way to create objects is to use constructor function. This way is actually more familiar to people with java or php5 background.
function Ship(model) {
 this.model = model;
 this.wings = 'the wings';
 this.body  = 'the body';
 this.jet   = 'the jet';
 this.guns  = 'the guns';
 this.fire  = function() {
  return this.guns + ' start to fire';
 };
 this.getModel = function(){return this.model};
}
var shipOne = new Ship(1);
var shipTwo = new Ship(2);
We can also create a new object in this way, although it looks a little wierd:
var shipOne = new Ship(1);
var shipTwo = new shipOne.constructor(2);
This works because constructor property refers to a function and we can call this function to return a new object. We use instanceof to tell an object's type: if (shipTwo instanceof Ship){}; In javascript, object is passed by reference, same to most of other OOP langauges. Global Object In javascript, the host environment provides a global object and all global variables are actually properties of the global object. If your host environment is the web browser, the global object is called window.