目录
- const person = {
- name: 'why',
- age: 18,
- height: 1.88,
- // 这种的就要加上''号
- 'my friend': {
- name: 'kobe',
- age: 30
- },
- run: function () {
- console.log('running');
- },
- eat: function () {
- console.log('eat foods');
- },
- };
- // 直接花括号赋值即可,用的最多
- const obj1 = {
- name: "why"
- }
- // 当函数被new关键词调用时,该函数就被称为构造函数
- const obj = new Object()
- obj.name = "star"
- obj2.runing = function(){}
- class Person {
- constructor(name) {
- this.name = name;
- }
- }
- const stu = new Person('star');
- const info = {
- name: "star",
- age: 18,
- friend: {
- name: "coder",
- age: 30
- },
- running: function() {
- console.log("running~")
- }
- }
- console.log(info.name)
- console.log(info.friend.name)
- info.running()
- info.age = 25
- info.running = function() {
- alert("I am running~")
- }
- console.log(info.age)
- info.running()
- info.height = 1.88
- info.studying = function() {
- console.log("I am studying~")
- }
- delete info.age
- delete info.height
- const obj = {
- name: "why",
- "my friend": "kobe",
- "eating something": function() {
- console.log("eating~")
- }
- }
-
- // 这里 . 语法不能用
- console.log(obj["my friend"])
-
- console.log(obj.name)
- // 这和 . 语法一个意思
- console.log(obj["name"])
-
- // 用变量的值充当对象的key
- var eatKey = "eating something"
- obj[eatKey]()
-
- // 可连在一起使用
- obj["eating something"]()
- // Object.keys() => 拿到对象的所有key
- const infoKeys = Object.keys(info)
- for (let i = 0; i < infoKeys.length; i++) {
- // 拿到key的值
- let key = infoKeys[i]
- // 拿到value的值
- let value = info[key]
- console.log(`key: ${key}, value: ${value}`)
- }
- // 可直接拿到对象的key
- for (let key in info) {
- let value = info[key]
- console.log(`key: ${key}, value: ${value}`)
- }
js代码可以运行在浏览器,也可运行在node环境,无论是什么环境,最终都是运行在内存中
内存会映射在真实的电脑物理内存中,所以内存越大,跑的速度越快~~~
- const obj1 = {}
- const obj2 = {}
- console.log(obj1 === obj2) // false
- const info = {
- name: "why",
- friend: {
- name: "kobe"
- }
- }
-
- const friend = info.friend
- friend.name = "james"
- console.log(info.friend.name) // james
- function foo(a) {
- a = 200
- }
- const num = 100
- foo(num)
- console.log(num) // 100
- function foo(a) {
- a = {
- name: "star"
- }
- }
- const obj = {
- name: "obj"
- }
- foo(obj)
- console.log(obj) //{ name:obj }
- function foo(a) {
- a.name = "star"
- }
-
- const obj = {
- name: "obj"
- }
- foo(obj)
- console.log(obj) // {name : star}
有点小蠢~
- const stu1 = {
- name: 'star',
- age: 16,
- height: 1.66,
- running: function () {
- console.log('running~');
- }
- };
- const stu2 = {
- name: 'coder',
- age: 17,
- height: 1.77,
- running: function () {
- console.log('running~');
- }
- };
- const stu3 = {
- name: 'liuli',
- age: 18,
- height: 1.88,
- running: function () {
- console.log('running~');
- }
- };
还是有点蠢~
- const nameArr = ['star', 'coder', 'liuli'];
- const ageArr = [16, 17, 18];
- const heightArr = [1.66, 1.77, 1.88];
- const funcs = {
- running: function () {
- console.log('running~');
- }
- };
-
- for (let i = 0; i < nameArr.length; i++) {
- let stu = {
- name: nameArr[i],
- age: ageArr[i],
- height: heightArr[i],
- running: funcs.running
- };
- console.log(stu); //{name: 'star', age: 16, height: 1.66, running: ƒ} ...
- }
- // 工厂函数(工厂生产student对象) -> 一种设计模式
- // 通过工厂设计模式, 自己来定义了一个这样的函数
- function createStudent(name, age, height) {
- const stu = {};
- stu.name = name;
- stu.age = age;
- stu.height = height;
- stu.running = function () {
- console.log('running~');
- };
- return stu;
- }
-
- const stu1 = createStudent('stare', 16, 1.66);
- const stu2 = createStudent('coder', 17, 1.77);
- const stu3 = createStudent('liuli', 18, 1.88);
- console.log(stu1);
- console.log(stu2);
- console.log(stu3);
弊端 : 拿到的数据的类型都是Object类型
简单理解下构造函数
- // JavaScript已经默认提供给了我们可以更加符合JavaScript思维方式(面向对象的思维方式)的一种创建对象的规则
- // 在函数中的this一般指向某一个对象
- /*
- 如果一个函数被new操作符调用
- 1.创建出来一个新的空对象
- 2.这个对象的 __proto__ 指向构造函数的 prototype
- 3.让this指向这个空对象
- 4.执行函数体的代码块
- 5.如果没有明确的返回一个非空对象, 那么this指向的对象会自动返回
- */
- function Coder(name, age, height) {
- // 相当于new操作符做了
- // let obj = {}
- // this = obj
- this.name = name
- this.age = age
- this.height = height
- this.running = function() {
- console.log("running~")
- }
- // return this
- }
-
- // 在函数调用的前面加 new 关键字(操作符)
- const stu1 = new coder("why", 18, 1.88)
- const stu2 = new coder("kobe", 30, 1.98)
- console.log(stu1, stu2)