批改状态:未批改
老师批语:
javascript之创建对象的方式
主要知识点
1)对象字面量
2)自定义构造函数
3)数组字面量
代码
对象字面量与自定义构造函数
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>test</div>
</body>
<script>
// 创建对象的方式(字面量或构造函数)
// 字面量
var cat = {
name: '喵喵怪',
say:function(){
return 'cat is called '+ this.name;
}
};
console.log(cat.say());
// 构造函数
var Person = function(name){
this.name = name;
this.run = function(){
return 'i am run but '+this.name+' not';
}
}
var tom = new Person('tom');
console.log(tom.run());
</script>
</html>运行结果

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号