这 6 点知识让我对 JS 的对象有了更进一步的了解

1. 对象方法 & this

方法只是保存函数值的属性。

简单对象方法

let rabbit = {};
rabbit.speak = function(line) {
    console.log("小兔子说: "+ line );
};
rabbit.speak("我还活着。")

输出:

T小兔子说: 我还活着。

对象方法 & this

当一个函数作为方法被调用时,对象会将函数作为属性并立即调用,就像在object.method()中一样,其主体中的特殊变量this将指向被调用的对象。


function speak(line) {
  console.log(this.type + "小兔子说:" + line)
};
let whiteRabbit = {type: "白色", speak: speak}

whiteRabbit.speak("噢,我真可爱!")

输出:

白色小兔子说:噢,我真可爱!

apply & call


function speak(line) {
  console.log(`${this.type}的小兔子说:${line}` );
};
let whiteRabbit = {type: "白色", speak: speak};

speak.apply(whiteRabbit, ["你这个小坏蛋!"]);
speak.call({type: "黑色"}, "嘿嘿,我不坏,你不爱!");
白色的小兔子说:你这个小坏蛋!
黑色的小兔子说:嘿嘿,我不坏,你不爱!

2.Prototype(原型)

空对象的原型

原型链最终的指向是Object的prototype, 而Object中的__proto__是null

let empty = {};
console.log(empty.toString);
console.log(empty.toString());

输出:

[Function: toString]
[object Object]

其他对象(数组、函数等等)的默认属性

console.log(Object.getPrototypeOf(isNaN) ==
            Function.prototype);
console.log(Object.getPrototypeOf([]) ==
Array.prototype);

输出:

true
true

Object.create 创建具有特定原型的对象

let protoRabbit = {
  speak: function (line) {
    console.log(`${this.type}兔子说:${line}` );
  }
}

let killerRabbit = Object.create(protoRabbit)
killerRabbit.type = '杀手'
killerRabbit.speak('准备受死吧!')

输出:

杀手兔子说:准备受死吧!

3.构造函数

— 构造函数原型


function Rabbit(type) {
    this.type = type;
}

let killerRabbit = new Rabbit("killer");
let blackRabbit = new Rabbit("black");
console.log(blackRabbit.type);

输出:

black

— 默认情况下,构造函数具有Object.prototype


function Rabbit(type) {
  this.type = type;
}

let blackRabbit = new Rabbit("黑色");
Rabbit.prototype.speak = function(line) {
  console.log(`${this.type}的兔子说:${line}` );
};
blackRabbit.speak("Boom...一波王炸!");

输出:

黑色的兔子说:Boom...一波王炸!

4. 重写派生属性

— 相同的原型名称


function Rabbit(type) {
    this.type = type;
}
let blackRabbit = new Rabbit("black");
let killerRabbit = new Rabbit("killer");

Rabbit.prototype.teeth = "small";
console.log(killerRabbit.teeth);
// small
killerRabbit.teeth = "long, sharp, and bloody";
console.log(killerRabbit.teeth);
// long, sharp, and bloody
console.log(blackRabbit.teeth);
// small
console.log(Rabbit.prototype.teeth);
// small

下面 console.log(blackRabbit.teeth)的结果是small,因为blackRabbit对象不具有teeth属性,它继承自Rabbit对象自己的teeth属性,值为 small。

5. 原型的干扰

— 可枚举与不可枚举

let map = {}

function storePhi(event, phi) {
  map[event] = phi
}

storePhi('pizza', 0.069)
storePhi('touched tree', -0.081)

Object.prototype.nonsense = 'hi'

for(let name in map) {
  console.log(name)
}

console.log('nonsense' in map)
console.log('toString' in map)

输出结果:

pizza
touched tree
nonsense
true
true

toString没有出现在for/in循环中,但是in运算符中返回true,这是因为 JS 区分可枚举属性不可枚举属性

我们通过简单分配创建的所有属性都是可枚举的,Object.prototype中的标准属性都是不可改变的,这就是为什么它们不出现在这样的for/in循环中的原因。

let map = {};
function storePhi(event, phi) {
    map[event] = phi;
}

storePhi("pizza", 0.069);
storePhi("touched tree", -0.081);

Object.defineProperty(Object.prototype, "hiddenNonsense",
                {enumerable: false, value: "hi"})

for (var name in map) {
    console.log(name)
}

console.log(map.hiddenNonsense)

输出:

pizza
touched tree
hi

通过使用Object.defineproperty函数可以定义自己的不可枚举属性,该函数允许我们控制要创建的属性的类型,在该示例中,hiddenNonsense在 map 中,但在 for...in 中不会显示。

— hasOwnProperty vs in 操作符

const map = {}
console.log("toString" in map)
console.log(map.hasOwnProperty("toString"))

输出:

true
false

hasOwnProperty方法告诉我们对象本身是否具有该属性,而无需查看其原型,这通常是比in运算符提供给我们的信息更有用的信息。

因此,如果你对基础对象原型感到困惑时,建议你可以这样写for/in循环:

for (var name in map) {
    if (map.hasOwnProperty(name)) {
        // ... this is an own property
    }
}

6.无原型对象

Object.create函数使我们能够创建具有特定原型的对象。我们还可以传递null作为原型,用来创建不带原型的新对象。

因此,我们不再需要hasOwnProperty,因为对象拥有的所有属性都是它自己的属性。现在,无论人们对Object.prototype做了什么,我们都可以安全地使用for/in循环

var map = Object.create(null);
map["pizza"] = 0.069;
console.log("toString" in map);
// false
console.log("pizza" in map);
// true

人才们的 【三连】 就是小智不断分享的最大动力,如果本篇博客有任何错误和建议,欢迎人才们留言,最后,谢谢大家的观看。


作者:Valentino Gagliardi 译者:前端小智 来源:valentinog

原文:https://medium.com/javascript-in-plain-english/six-things-you-should-know-about-objects-in-javascript-ccd11a9e1998

展开阅读全文

页面更新:2024-03-17

标签:对象   数组   原型   变量   函数   兔子   实例   属性   杀手   白色   黑色   名称   参数   可爱   方法   知识   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top