<html>
<head>
<script>
window.onload = function(){
//定义一个变量,作为命名空间
var myObj = {};
//给变量的属性赋予一个类似于构造函数似的函数,变量名称大写
myObj.Person = function(name, sex, age){
this.name = name;
this.sex = sex;
this.age = age;
}
//给Person对象设置prototype
myObj.Person.prototype = {
getName : function(){
return this.name;
},
setName : function(name){
this.name= name;
}
}
//创建实例
var person = new myObj.Person('szq', 'man', 22);
//调用方法,打印日志
console.log(person.getName());
person.setName('wy');
console.log(person.getName());
}
</script>
</head>
</html>