关于JavaScript 的对象的介绍是怎样的

发布时间:2021-09-30 14:42:27 作者:柒染
来源:亿速云 阅读:129

本篇文章为大家展示了关于JavaScript 的对象的介绍是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

单个兔子对象(如杀手兔子)包含仅适用于自身的属性(在本例中为type),并从其原型派生共享属性

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

上述内容就是关于JavaScript 的对象的介绍是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. JavaScript Date的对象简介是怎样的
  2. JavaScript语法介绍是怎样的

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript

上一篇:如何使用批处理实现进度条效果

下一篇:如何编写JavaScript实现下拉菜单的显示隐藏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》