Home > Javascript > JavaScript (2)

JavaScript (2)

第五周主要學object、class和constructor。

1. Declaring objects:

  • var objName = {};   //Literal notation
  • var objName = new Object();   //Constructor notation
  • var objName = new constructorName();

2. Declaring properties in objects:

  • var objName = {
    prop1 : “123”,
    prop2 : true
    };
  • var objName = new Object();
    objName.prop1 = “123”;
    objName.prop2 = true;
  • var objName = new Object();
    objName [“prop1”] = 123;
    objName [“prop2”] = true;
  • var objName = new Object();
    var a = “prop1”;
    var b = “prop2”;
    objName[a] = 123;
    objName[b] = true;

3. Declaring methods in objects:

  • var objName = {
    method1: function(){…},
    method2: function(){…} }
    ;
  • objName.methodName = function(X){this.prop1=X;};
    //在Object中的Method,可以「this」替代Object名稱,令同一method可以套用至不同objects中

4. Using constructors to build objects:

  • var obj = new Object(); var arry = new Array();
    //以「Object」及「Array」製造新Object及Array,預設constructor「Object」及「Array」沒有parameters
  • function ABC(a,b,c){ this.a=a; this.b=b; this.c=c; } var obj = new ABC(1,2,3); //宣告Constructor「ABC」,可加任意parameters,注意最後無分號,而Constructor Notation「new」是製造新Object時的關鍵詞
    //宣告Constructor「ABC」,實際上是建立並定義一個新class「ABC」,class類似於Javascript中的type如number及string

5. Others about objects:

  • Object屬於格式的一種,可以用於Array中,譬如: var arry = new Array(); arry[0] = new Object(); arry[1] = new Object();
  • Object可以用作parameter,譬如: var ABC = function(obj){ obj.prop1 = “string”; obj.prop2 = true; };
  • Object有所謂「baggage」,隱藏的參數和properties,如objName.hasOwnProperty(‘propName’)會檢查objName有沒有propName的這個property並傳回真假值

6. For-in loop:

  • var objName = {};
    for ( var propName in objName ){console.log(propName);}
    for ( var propName in objName ){console.log(objName[propName]);}
    //前者列出所有property名稱,後者列出所有property的value

7. Object-oriented programming(OOP) and class:

  • class 可以理解為Javascript中的type,或者objects的category,如numbers和strings,constructors用於建立新的class
  • className.prototype.newMethod = function(){…};
    //宣告一個同一class通用的method
  • obj1.prototype = new obj2();
    //class之間可以「繼承」(inherit)其他class的特質,例子中obj1會繼承obj2的obj1自身沒有定義的properties和methods,如obj1自身已定義則採用obj1自身的properties和methods
    //class的「繼承」有連續性,可以不斷向上層追溯,直到找到相應的properties和methods為止

8. Hidden variables:

  • function Person(name){
    this.name = name;
    var money = 0;
    this.askPW = function(pw) {if  ( pw = “1234”) {return money;}} ;
    }
    //name是公開的,而money是隱藏的,只有透過正確的「pw」才會傳回money的值

Literal notation

Categories: Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: