javascript-Object類型

來源:互聯網
上載者:User

標籤:splay   nop   ret   func   log   建構函式   關聯陣列   類型   isp   

Object的建立

var obj1 = new Object();  // 建構函式方式建立var obj2 = {};  // 對象字面量方式建立

使用建構函式建立的區別

var obj1 = new Object(100 + 1);alert(obj1);  // 101var obj1 = new Object(100);obj1 += 100;alert(typeof obj1);  // "number"function num() {return 100;}var obj1 = new Object(num);alert(obj1());  // 100alert(typeof obj1);  // "function"

Object的賦值

var obj1 = new Object();obj1.name = "Jack";obj1.sayName = function () {    return this.name;};obj1["year age"] = "1992 25";  // 關聯陣列賦值alert(obj1["year age"]);  // "1992 25"var obj2 = {  // 動態賦值也可以用於字面量    name : "Eson",    sayName : function () {        return this.name;    },    "year age" : "1992 25"};alert(obj2["year age"]);  // "1992 25"

匿名對象

function displayInfo(obj) {    return obj.name + " " + obj.age;}alert(displayInfo({    name : "Nopi",    age : 25}));  // "Nopi 25"

枚舉對象的屬性

var obj2 = {    name : "Jack",    age : 25};with (obj2) {    var str = "姓名:" + name + "\n";    str += "年齡:" + age;}alert(str);  // "姓名:Jack 年齡:25"var obj2 = {    name : "Jack",    age : 25};for (var p in obj2) {    alert(p);  // "name"  "age"}

 

javascript-Object類型

聯繫我們

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