JavaScript && OOP

來源:互聯網
上載者:User

前段時間,在Team裡面做的一個關於JavaScript和OOP的topic,終於記得帶過來,share出來!

1. Data Structure
JavaScript中的資料很簡潔的。簡單資料只有 undefined, null, boolean, number和string這五種,而複雜資料只有一種,即object. 這就好比中國古典的樸素唯物思想,把世界最基本的元素歸為金木水火土,其他複雜的物質都是由這五種基本元素組成。

JavaScript中的代碼只體現為一種形式,就是function.

任何一個JavaScript的標識、常量、變數和參數都只是unfined, null, boolen, number, string, object 和 function類型中的一種,也就typeof傳回值表明的類型。除此之外沒有其他類型了。

2. Object.  所謂“對象化”,就是可以將資料和程式碼群組織成複雜結構的能力。JavaScript中只有object類型和function類型提供了對象化的能力。

所謂“函數的對象化能力”,是指任何一個函數都可以為其動態地添加或去除屬性,這些屬性可以是簡單類型,可以是對象,也可以是其他函數。
也就是說,函數具有對象的全部特徵,你完全可以把函數當對象來用。其實,函數就是對象,只不過比一般的對象多了一個括弧“()”操作符,這個操作符用來執行函數的邏輯。
JavaScript使用了一種被稱為JavaScript Object Notation(縮寫JSON)的形式,翻譯為中文就是“JavaScript對象標記法”。

    var company =
    {
        name: "Microsoft",
        product: "softwares",
        chairman: {name: "Bill Gates", age: 53, Married: true},
        employees: [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}],

        readme: function() {document.write(this.name + " product " + this.product);}

    };
3. OOP
1)Encapsulation.
var Company = function(name,product){
                var temp=”temp”;
        this.name= name;
        this.product= product;
        this.chairman= {name: "Bill Gates", age: 53, Married: true};
       this.employees= [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}];

        this.readme= function() {document.write(this.name + " product " + this.product);};

};
var company = new Company(“Microsoft”,”Inventory”);

2) InheritPrototype: prototype源自法語,軟體界的標準翻譯為“原型”,代表事物的初始形態,也含有模型和樣板的意義。JavaScript的所有function類型的對象都有一個prototype屬性。這個prototype屬性本身又是一個object類型的對象,因此我們也可以給這個prototype對象添加任意的屬性和方法。既然prototype是對象的“原型”,那麼由該函數構造出來的對象應該都會具有這個“原型”的特性。

  function Person(name)   //基類建構函式
     {
         this.name = name;
     };
    
     Person.prototype.SayHello = function()  //給基類建構函式的prototype添加方法
     {
         alert("Hello, I'm " + this.name);
     };
    
    function Employee(name, salary) //子類建構函式
    {
        Person.call(this, name);    //調用基類建構函式
        this.salary = salary;
    };
    
    Employee.prototype = new Person();  //建一個基類的對象作為子類原型的原型
    
    Employee.prototype.ShowMeTheMoney = function()  //給子類添建構函式的prototype添加方法
    {
        alert(this.name + " $" + this.salary);
    };

    var BillGates = new Person("Bill Gates");   //建立基類Person的BillGates對象
    var SteveJobs = new Employee("Steve Jobs", 1234);   //建立子類Employee的SteveJobs對象

    BillGates.SayHello();       //通過對象直接調用到prototype的方法
    SteveJobs.SayHello();       //通過子類對象直接調用基類prototype的方法,關注!
SteveJobs.ShowMeTheMoney(); //通過子類對象直接調用子類prototype的方法

3)
Polymorphismfunction Shape(margin)

    {
        this.margin =margin;
        this.size=function(){document.write("I am Original. my margin is:"+this.margin+'<br>');};

        this.hello=function(){document.write("My margin is:"+this.margin+'<br>');}

    };
    
                function Circle(margin)
                {              
                                Shape.call(this,margin);
                                this.size = function(){document.write("I am Circle! my size is :"+3.14*this.margin*this.margin+'<br>');}

                }
                Circle.prototype=new Shape();

                 function Rectangle(margin)
                {              
                                Shape.call(this,margin);
                                this.size = function(){document.write("I am rectangle! my size is: "+this.margin*this.margin+'<br>');}

                }
                Rectangle.prototype = new Shape();

    var shape = new Shape(100);
                shape.size();

                var circle = new Circle(100);
                circle.size();
                circle.hello();

                var rect = new Rectangle(100);
                rect.size();
                rect.hello();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.