24h購物| | PChome| 登入
2012-05-06 10:47:01| 人氣354| 回應0 | 上一篇 | 下一篇

[JavaScript] Introduction to Objects I

推薦 0 收藏 0 轉貼0 訂閱站台

Objects In Review

var spencer = {
  age: 22,
  country: "United States"
};

// make spencer2 here with constructor notation
var spencer2 = new Object();
spencer2.age = 22;
spencer2.country = "United States";


var snoopy = new Object();
snoopy.species = "beagle";
snoopy.age = 10;

// save Snoopy's age and species into variables
// use dot notation for snoopy's species
var species = snoopy.species;
   
// use bracket notation for snoopy's age
var age = snoopy.age;


function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
        
    };
    // define a perimeter method here
    this.perimeter = function() {
        return Math.PI * 2 * this.radius;
    }
};


// 3 lines required to make harry_potter
var harry_potter = new Object();
harry_potter.length = 350;
harry_potter.author = "J.K. Rowling";

// A custom constructor for book
function Book (length, author) {
    this.length = length;
    this.author = author;
}

// Use our new constructor to make the_hobbit in one line
var the_hobbit = new Book(320, "J.R.R. Tolkien");


台長: Morris
人氣(354) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: [學習]JavaScript |
此分類下一篇:[JavaScript] Introduction to Objects II
此分類上一篇:[JavaScript] Conditionals in JavaScript

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文