ITeye博客

1.0.4 js中的对象创建

listen-raining · 2016-02-03 11:07 · JavaScript · Web前端 · Markdown

1.0 对象属性 2.0 创建对象有两种方式: 2.0.1 定义并创建对象的实例 2.0.2 使用函数来定义对象,然后创建新的对象实例

			//2 使用函数来定义对象,然后创建新的对象实例
			var myperson=new myPerson("Bill","Gates",56,"blue");
			myperson.changeName("张");
			alert(myperson.firstname + " " + myperson.lastname);
			
		}
		
		//使用函数来定义对象,然后创建新的对象实例
		function myPerson(firstname,lastname,age,eyecolor)
		{
			this.firstname=firstname;
			this.lastname=lastname;
			this.age=age;
			this.eyecolor=eyecolor;
			
			//添加方法
			this.changeName=changeName;
			function changeName(name){
				this.firstname=name;
			}
		}
		
		
	</script>
</head>
<body>
	<p id="demo1" onclick="showlength()">字符串长度为:<p>
	<button onclick="createObject()">创建对象</button>
</body>
                                                       @dianxinxinxiyuan.xiuyanxilu.pudongqu.shanghai

评论(0)

暂无评论。

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