ITeye博客

面向对象

djob2008 · 2009-12-11 12:59 · javaScript · Web前端 · JavaScript · Markdown

1/Creating objects using new Object()

<script language="javascript" type="text/javascript">
<!--

person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"

person.run = function() {
	this.state = "running"
	this.speed = "4ms^-1"
}

//-->
</script>

 

2/Creating objects using Literal Notation

<script language="javascript" type="text/javascript">
<!--

// Object Literals

timObject = {
	property1 : "Hello",
	property2 : "MmmMMm",
	property3 : ["mmm", 2, 3, 6, "kkk"],
	method1 : function(){alert("Method had been called" + this.property1)}
};

timObject.method1();
alert(timObject.property3[2]) // will yield 3

var circle = { x : 0, y : 0, radius: 2 } // another example

// nesting is no problem.
var rectangle = { 
	upperLeft : { x : 2, y : 2 },
	lowerRight : { x : 4, y : 4}
}

alert(rectangle.upperLeft.x) // will yield 2

//-->
</script>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。