April 10th, 2009
Javascript provides flexible constructor. Below are sample code that implement the flexible constructor:
var Student = function(_department, _course, _year){
department = _department || "";
course = _course || "";
year = _year || "";
this.set_department = function(_department){
department = _department;
}
this.set_course = function(_course){
course = _course;
}
this.set_year = function(_year){
year = _year;
}
this.display_information = function(){
console.log("Information of the student:");
console.log("Department: " + department);
console.log("Course: " + course);
console.log("Year: " + year);
}
}
//constructor
var obj_student = new Student("Computer Information Scince", "ICT", "2");
obj_student.display_information();
var another_obj_student = new Student();
another_obj_student.set_department("Technology");
another_obj_student.set_course("BIS");
another_obj_student.set_year("Foundation");
another_obj_student.display_information();
Sample output:
Information of the student:
Department: Computer Information Scince
Course: ICT
Year: 2
Information of the student:
Department: Technology
Course: BIS
Year: Foundation |
Tags: Javascript
Posted in Javascript | No Comments »
April 6th, 2009
Inheritance in javascript can be implemented easily using the concept of mixin. Below is an exmple to demonstrate javascript inheritance:
ClassA = function(){
this.display = function(){
console.log("here's a display function in ClassA");
}
this.str = function(){
return "string in ClassA";
}
}
ClassB = function(){
ClassA.call(this);
//overriden method
this.str = function(){
return "string in ClassB";
}
}
//instantiate object of ClassB
obj = new ClassB();
//calling display method from ClassA
obj.display();
//this will invoke str method in ClassB NOT in ClassA
console.log(obj.str());
| here’s a display function in ClassA |
| string in ClassB string in ClassB |
Tags: Javascript
Posted in Javascript | No Comments »
April 6th, 2009
As we are working with object or array, sometimes, we need to delete it’s property of element. Below is an example of a simple way to do it.
var display_before = function (str){
return "before delete " + str;
}
var display_after = function (str){
return "after delete " + str;
}
var obj = {x:12, y:15};
console.log(display_before("obj.x") + " " + obj.x);
//delete property x from object
delete obj.x;
console.log(display_after("obj.x") + " " + obj.x);
//delete array[2]
var array = [2, 4, 6, 8, 10]
console.log(display_before("array: ") + array);
array.splice(2,1); //delete value 6 from array
console.log(display_after("index 2 from array: ") + array);
Sample result (in firebug plugin):
before delete obj.x 12
after delete obj.x undefined
before delete array: 2,4,6,8,10
after delete index 2 from array: 2,4,8,10 |
Tags: Javascript
Posted in Javascript | No Comments »
March 23rd, 2009
C++ allows developers to use multiple inheritance. Unlike C++, Ruby only allows developers to use single inheritance. But Ruby has mixin functionality, which allows code to be included from other modules.
Here’s sample code to demonstrate mixin:
module MixinFirst
def mixin_first_display
puts "This is mixin first"
end
end
module MixinSecond
def mixin_second_display
puts "This is mixin second"
end
end
class Base
def inheritance_display
puts "This is inheritance"
end
end
class Child < Base
include MixinFirst
include MixinSecond
end
child = Child.new
child.mixin_first_display
child.mixin_second_display
child.inheritance_display
Sample result:
This is mixin first
This is mixin second
This is inheritance |
|
Tags: Ruby
Posted in Ruby & Rails | No Comments »
March 20th, 2009
Inheritance is one of Object-Oriented concept. Since Ruby is an Object-Oriented Programming, it has implemented the Inheritance functionality.
#intialize global variable
$value = 0
class Parent
def parent_value
$value = 5
end
end
class Child < Parent
def child_value
$value = 10
end
end
child = Child.new
#calling parent_method using child object
base_value = child.parent_value
#calling child_method
derive_value = child.child_value
#display result
puts "Here's parent value: #{base_value}"
puts "Here's child value: #{derive_value}"
Sample Result:
Here’s parent value: 5
Here’s child value: 10 |
Tags: Ruby
Posted in Ruby & Rails | 1 Comment »
March 20th, 2009
class Encapsulation
def get_value
@value
end
def set_value value
@value = value
return nil
end
end
encapsulation = Encapsulation.new
encapsulation.set_value(15)
puts encapsulation.get_value
In this code, you cannot mess around with the state of its variable @value. The only way you can access it is through get & set method (get_value, set_value). This is the encapsulation
Sample Result
Tags: Ruby
Posted in Ruby & Rails | No Comments »
March 19th, 2009
Here just how’s to declare global, class, instant, and local variables in Ruby:
$global_variable = "global variable"
class First
@@class_variable = 0
def display_global_variable
puts "#{$global_variable} access from class 'First'"
end
def class_variable
@@class_variable += 1
puts "value of class variable : #{@@class_variable}"
end
end
class Second
def initialize(id,name)
@id = id # id is a local variable
@name = name # name is a local variable
end
def display_global_variable
puts "#{$global_variable} access from class 'Second'"
end
def display_instance_variable
puts "display value of instance variable id = #{@id}, name = #{@name} "
end
end
#instantiate object
first = First.new
second = Second.new(1, "borey")
#Demonstrate on class variable
first.display_global_variable
second.display_global_variable
puts "\n"
#Demonstrate on class variable
first.class_variable
first.class_variable
puts "\n"
#Demonstrate on instance variable
second.display_instance_variable
Here’s the result after code is executed:
global variable access from class ‘First’
global variable access from class ‘Second’
value of class variable : 1
value of class variable : 2
display value of instance variable id = 1, name = borey |
Tags: Ruby
Posted in Ruby & Rails | No Comments »
March 4th, 2009
In object-oriented programming, the open/closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”; that is, such an entity can allow its behavior to be modified without altering its source code.
Modules that conform to the open-closed principle have two primary attributes:
- They are “Open For Extension”. This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.
- They are “Closed for Modification”. The source code of such a module is inviolate. No one is allowed to make source code changes to it.
In the implementation of this principle, normally, the abstract interfaces are used.
Posted in Design Principle | No Comments »
February 27th, 2009
Today, just trying to find gedit’s alternative editor. gedit is an editor running on Linux which is good for coding Javascript as it has many useful plug in. Since my workstation at home doesn’t run on Linux, I have find another alternative to gedit. After googling about a half hour, I found out about jEdit. After installing jEdit & install some extra plug in, i can see that jEdit is good as compare to gedit.
Posted in Useful stuff | No Comments »
February 9th, 2009
Since i’m going to write some code for the client side, I have to start learning OO javascript. What makes me surprise the most is how it is written. here are some codes i have tested:

as you can see in the code, i only create one function, however i can call in different ways. This is how dynamic Javascript is. Really Really dynamic!!!!

Posted in Javascript | No Comments »