簡體   English   中英

如何將屬性添加到原型(javascript)

[英]How to add property to a prototype (javascript)

我想弄清楚如何制作

normalFunction.prototype.helloWorld = 'hello world'

function normalFunction(){
 
}

function helloWorld() {
  return 'hello world'
}

對於問題normalFunction必須是 function。

創建您的 object

const normalFunctionPrototype = {
    helloWorld: () => 'hello world'
}

以您的第一個 object 作為原型創建一個新的 object

let normalFunction = Object.create( normalFunctionPrototype );

我們現在可以調用它的方法

console.log( normalFunction.helloWorld() ) // hello world

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM