javascript物件導向

來源:互聯網
上載者:User

在JavaScript 中參考型別是一種資料結構,將資料和功能組織在一起。它也常被稱做為類,但JavaScript 中卻沒有類的概念。雖然JavaScript 是一門物件導向的語言,卻不具備傳統物件導向語言所支援的類和介面等基本結構。

如何建立一個數組,在script中建立數組有三種方式:

1.new運算子

var  user = new objcet();user.name = "張三";user.age=20;user.sex="男';

2.對象字面量

var user ={       name = "張三",       age = 20,       sex = "男" }
注意:使用字面量建立對象的時候,以","結束,但是最後一個屬性後不加逗號.

3.傳統複雜方式

var user = {}; user.name = "張三"; user.age = 20; user.sex = "男";

對象的調用方式

對象的調用方式有兩種:

方法1:

alert(user.name + " " +user.age);  //返回 '張三 20'

方法2:

alert(user['name'] + " " +user['age']);  //返回 '張三 20'

添加方法

var user = {      name:"張三",      age:20,      sex:"男",      info:function(){        alert(this.name+"        "+this.age+"      "+this.sex);      },      showHello:showHello//將對象外部的方法添加到對象}    function showHello(){        alert("Hello!");        }    user.info();         //兩種調用方法   user.showHello();


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.