`
yahaitt
  • 浏览: 756080 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

Javascript对象构建

阅读更多
一、Javascript对象构建构建javascript对象的方式有多种,其中,如下应用了property方式:
<script language="javascript">
	S = function(){
				
	};
	S.prototype = {
		username : 'wujinli',
		age : 26,
		address : 'hangzhou',
		setUsername : function(username){
			this.username = username;
			return this;
		},
		setAge : function(age) {
			this.age = age;
			return this;
		},
		setAddress : function(address) {
			this.address = address;
			return this;
		}
	};
	var s = new S();
	alert(s.username);
	s.setAge(20);
	alert(s.age);
</script>

二、prototype顺序问题:
如果按
S.prototype = {...}之后又要更新属性(增加属性或更新属性值),则可以在后面再继续添加,如:
S.prototype.info = "userinfo";

而如果以上顺序相反的话,则S.prototype.info属性是无效的。也就是相当于S.prototype = {...}是初始化属性,之前的所有属性都将先被清除。
分享到:
评论
1 楼 gaoyide 2008-07-31  
我沙发 

相关推荐

Global site tag (gtag.js) - Google Analytics