javascript 使用Object.create

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用Object.create相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>JavaScript Sandbox: Section 5</title>
</head>
<body>
  <h1>JavaScript Sandbox: Section 5</h1>

  <script src="app.js"></script>
</body>
</html>
const personPrototypes = {
  greeting: function() {
    return `Hello there ${this.firstName} ${this.lastName}`;
  },
  getsMarried: function(newLastName) {
    this.lastName = newLastName;
  }
}

const mary = Object.create(personPrototypes);
mary.firstName = 'Mary';
mary.lastName = 'Williams';
mary.age = 30;

mary.getsMarried('Thompson');

console.log(mary.greeting());

const brad = Object.create(personPrototypes, {
  firstName: {value: 'Brad'},
  lastName: {value: 'Traversy'},
  age: {value: 36}
});

console.log(brad);

console.log(brad.greeting());

以上是关于javascript 使用Object.create的主要内容,如果未能解决你的问题,请参考以下文章

类和原型之工厂模式!

javascript继承

一道笔试题引发的对Object.creat()的思考

JavaScript-其他设计模式

SetWinEventHook 与 CreateProcess, C++

JavaScript 使用JavaScript更改CSS(JavaScript)