1、原始创建
<script>
person = new Object();//不要var
person.firstname = "Bill";
person.lastname = "Gates";
person.age = 56;
person.eyecolor = "blue";
person.say = function () {
console.log(person.firstname);
}
person.say();//Bill
</script>
<script>
var person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue",
say: function () {
console.log(this.firstname);
}
};
person.say();//John
</script>
2、进阶版本
<script>
function CreatePserson(name) {
var person = new Object();
person.name = name;
person.sayHi = function () {
console.log(this.name);
}
return person;
}
var stu1 = CreatePserson("wu");
stu1.sayHi();//wu
</script>
3、最终版本
<script>
function Person(name, age) {
this.name = name;
this.age = age;
this.say = function () {
console.log(this.name + "===" + this.age);
}
}
var per = new Person("wu",27);
per.say();//wu===27
</script>