对象(Object)
样例代码:
var Ke = {
'name': 'Keafmd',
'age': 18,
address: '北京',
isEdu:false
}
console.log(Ke)
完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var Ke = {
'name': 'Keafmd',
'age': 18,
address: '北京',
isEdu:false
}
console.log(Ke)
</script>
</head>
<body>
</body>
</html>
效果截图:
对象的创建
使用 {} 创建
var person = {
name : 'Keafmd',
sayHi:function(){
console.log('hi, my name is :'+this.name)
}
};
样例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
var person = {
name : 'Keafmd',
sayHi:function(){
console.log('hi, my name is :'+this.name)
}
};
console.log(person)
person.sayHi()
</script>
<title></title>
</head>
<body>
</body>
</html>
效果截图:
使用 Object 创建
var p = new Object();
p.name = 'Keafmd';
p.age = 18;
console.log(p);
样例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
var p = new Object();
p.name = 'Keafmd';
p.age = 18;
console.log(p);
</script>
<title></title>
</head>
<body>
</body>
</html>
效果截图:
使用 Function 创建
function Student(){
this.name = '';
this.age = 0;
}
var stu1 = new Student();
stu1.name = "Keafmd";
stu1.age = 18;
stu1.address = '哈尔滨';
console.log(stu1);
var stu2 = new Student();
console.log(stu2);
样例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function Student(){
this.name = '';
this.age = 0;
}
var stu1 = new Student();
stu1.name = "Keafmd";
stu1.age = 18;
stu1.address = '哈尔滨';
console.log(stu1);
var stu2 = new Student();
console.log(stu2);
</script>
<title></title>
</head>
<body>
</body>
</html>
效果截图:
使用 class 关键字
class Human{
constructor(name) {
this.name = name;
}
sayHi(){
console.log('我是: '+this.name);
}
}
var h1 = new Human('Keafmd');
h1.sayHi()
样例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
class Human{
constructor(name) {
this.name = name;
}
sayHi(){
console.log('我是: '+this.name);
}
}
var h1 = new Human('Keafmd');
h1.sayHi()
</script>
<title></title>
</head>
<body>
</body>
</html>
效果截图:
对象的原型模型
样例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
function Student(){
this.name = '';
this.age = 0;
}
var s1 = new Student();
Student.prototype.sayHi = function(){
console.log('打招呼')
}
s1.sayHi();
var s2 = new Student();
s2.sayHi();
</script>
</head>
<body>
</body>
</html>
效果截图:
写作不易,读完如果对你有帮助,感谢点赞支持!
如果你是电脑端,看见右下角的 “一键三连” 了吗,没错点它 [哈哈]
加油!
共同努力!
Keafmd
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!