七、对象的基础知识

七、对象的基础知识

在JavaScript中,对象是一个无序属性的集合,任何事物都是对象。

我们现阶段研究的对象是具有属性和方法的数据。

属性是与对象相关的值。比如人身高、体重。

方法是能够在对象上执行的动作。比如人的吃饭、睡觉。

7.1 创建对象:字面量{}

k:v 每一项之间使用逗号隔开,最后一项不写逗号

1
2
3
4
5
6
7
8
9
10
11
12
// 创建一个对像
var person = {
    // 属性
    name : "小明",
    age : 18,
    // 方法
    getName : function(){
        // this,表示该对象person
        return this.name;
    }
}
console.log(typeof person); // object

7.2 读取对象:点语法,中括号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 读取 点语法
console.log(person.age); // 18
// 读取 中括号
console.log(person["name"]); // 小明

// 方法
getName : function(){
    // this,表示该对象person
    return this.name;
}


console.log(person.getName);
console.log(person.getName());

UAsGUx.png

7.3 设置: =

1
2
3
// 设置 =
person.sex = "男性";
console.log(person);

UAsJ56.png

点击查看

本文标题:七、对象的基础知识

文章作者:Mango

发布时间:2020年07月08日 - 22:13:33

最后更新:2020年07月08日 - 22:35:39

原始链接:https://mango185.github.io/post/1b0a66e8.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------