Java Script ,OOP

Now , this is something that is worthwhile to take a look and have a good understanding.

Most of most that use the higher level of API build on top of Asp.net 2.0, Will rarely use client side scripting, so many people that even start jump to asp.net but does not even know what html is. but really at the end what you eventually will produce is merely HTML,DHTML and JavaScript.

I bet those that talk a lot about this and that technology still lacking of the fundamental concept.

this is the trickiest interpreter language, lousiest.

  • Even Function return as Object,
  • every thing works with name value pair { xxx:value }
  • If you haven't declare a method or member of a class,the interpreter will create it for you and it becomes global , on window.youmember
 function test() {
        foo = "test";
    }
    test();
    alert(window.foo == "test");
  • You can change the Execution Context by functionname.call(context,param1,n)  or functionname.apply(context,param1,n)

When EcmaScript interpret your code, it has the internal [[scope]] property that make a chain scope, the last scope only contains the global object. Scope can be Global, belongs to the window object. or Private , belongs inside a function

The prototype also make a chain , if an object refer to another one object, it keeps searching for it.Lastly the script will try to get the value into string if you access the value. while it build the variable/activation object the interpreter add arguments property that you can access inside any function .

Closures

an inner function that use and manipulate the outer function variable member or parameter.because of this comes the private and public member

Public Member

You can set directly on the

constructor

   1:  Function user(name,age)
   2:   
   3:  {
   4:   
   5:  this.name=name;
   6:   
   7:  this.age=age;
   8:   
   9:  }
  10:   

or by attaching on prototype

user.prototype.getname()=function{return this.name;};

Private Member Go here

Static method

Object.Staticmethodname=function{//your code here}; 

Can Javascript do Inheritance and Polymorphism?

Yes , with a little help from some wrapper, You can choose:

  1. Crockford Helper to refer to the base class function use the this.uber('functionname')
  2. DeanEdwards Base Lib this is more readable, since like if we want to refer to the base class function , just use this.base() , it will figure out itself which method
  3. Prototype Library 

 Name Space and Packaging

  1. Dojo Framework: dojo.js 77kb, include Ajax, widget, namespace ,etc.check it out
  2. YUI. yahoo user interface

Share this post: | | | |
Published Tuesday, November 18, 2008 2:44 PM by cipto
Filed under:

Comments

# re: Java Script ,OOP

Tuesday, November 18, 2008 11:07 PM by cahnom

Cipto, gw pernah bahas salah satu penerapan OOP pada javascript.

Silakan cek di

www.masykur.web.id/.../object-oriented-javascript-overloading-dan-type-checking.aspx

# re: Java Script ,OOP

Wednesday, November 19, 2008 3:06 PM by cipto

yah bukan itu maksudnya mas... kalo yang dibuat di library helper itu bikin java script jadi oop,suatu object bisa inheritance dan extend.

Contoh over load

   function sendMessage(msg, obj) {

   //wah inilah kekayaan javascript yang dodol dan tidak ada validasi,

   //cara nentuin dia oper berapa param ya: pake arguments

       if (arguments.length == 2)

           obj.handleMsg(msg);

       else

           alert(msg);

   }

   sendMessage("hoi");

   sendMessage("overload", { handleMsg: function(msg) { alert("this is a custom message:" + msg) } });

//function to make arguments To array

   function argumentsToArray() {

       var arr = [];

       for (var i = 0; i < arguments.length; i++) {

           arr.push(argumentsIdea);

       }

       return arr;

   }

  //generic function to check the type of a function

   function strict(types, args) {

       if (types.length != args.length) {

           throw "Invalid number of arguments.expected " + types.length + ", received " +

           args.length + " instead.";

       }

       for (var i = 0; i < args.length; i++) {

           if (argsIdea.constructor != typesIdea) {

               throw "invalid argument type. Expected " + typesIdea.name + ",received " + argsIdea.constructor.name + " instead";                            

           }

       }

   }

# re: Java Script ,OOP

Wednesday, November 19, 2008 5:11 PM by cipto

pake library

 var Person = Base.extend({

       //pass in contructor

       constructor: function(name) { this.name = name; }

       //pass in method

   ,

       getName: function() { return this.name; }

   });

   var cipto = new Person("cipto");

   alert(cipto.getName());

   var User = Person.extend({

       constructor: function(name, password) { this.base(name) = name; this.password = password; },

       getPassword: function(){return this.password;}

   });