Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

Object.create()

JavaScript Object.create()方法

Object.create()方法用于创建具有指定原型对象和属性的新对象。我们可以通过Object.creates(空)来创建没有原型的对象。

语法:

Object.create(prototype[, propertiesObject])

参数

prototype:这是必须从中创建新对象的原型对象。
propertiesObject :这是一个可选参数。它指定要添加到新创建对象的可枚举属性。

返回

Object.create()返回具有指定原型对象和属性的新对象。 。

浏览器支持:

Chrome
Edge
Firefox
Opera

示例1

const people = {
  printIntroduction: function ()
   {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};
const me = Object.create(people);
me.name = "Marry"; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten
me.printIntroduction();
输出:
"My name is Marry。Am I human? true"

示例2

function fruits() {
this.name = 'franco';
}
       function fun() {
fruits.call(this)
 }
fun.prototype = Object.create(fruits.prototype);
const app = new fun();
console.log(app.name);
输出:
"franco"

示例3

function fruits() {
this.name = 'fruit';
this.season = 'Winter';
}
function apple() {
fruits.call(this);
}
apple.prototype = Object.create(fruits.prototype);
const app = new apple();
console.log(app.name,app.season);
console.log(app.season);
输出:
"fruit"
 "Winter"
"Winter"
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4