批改状态:合格
老师批语:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>javascript 创建对象</title>
</head>
<body>
<script>
// 1.通过Object创建对象
let person=new Object();
person.name="xiaopi";
person.age=26;
person.sex="male";
console.log(person.name + " is " + person.age + " years old.");
// 2.通过字面量创建对象
let person1={name:'xiaojun',age:22,sex:'female'};
console.log(person1);
// 2.通过构造函数创建对象
function person2() {
this.name='xiaohong';//通过this关键字设置默认成员
this.age='23';
this.sex='female';
this.Introduce=function(){
alert('MY name is '+this.name+' I\'m '+this.age+'years old '+this.sex);
};
console.log('MY name is '+this.name+' I\'m '+this.age+'years old '+this.sex);
}
let person3 = new person2();
person3.Introduce();
</script>
</body>
</html>点击 "运行实例" 按钮查看在线实例
总结:javascript中一切皆对象。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号