0
我試圖獲取delicious
和love
方法的名稱,然後將其放入數組quotes
。我需要這樣做,因爲報價將顯示在選擇框中。我嘗試使用Function.name
。它適用於Say
,但是當我在delicious
和love
上使用它時,我得到的是空字符串。無法返回對象方法名稱
這裏有什麼問題?
console.log('>>START')
function Say(name) {
this.name = name
}
function Food(name) {
this.name = name
}
Say.delicious = function(food) {
if (!food) food = 'food'
console.log('This '+food+' is delicious')
}
Say.love = function(food) {
if (!food) food = 'food'
console.log('I love '+food)
}
var pizza = new Food('Pizza')
var donut = new Food('Donut')
var quotes = [Say.delicious.name, Say.love.name]
Say.delicious(donut.name) // This Donut is delicious
Say.love(pizza.name) // I love Pizza
console.log(quotes) // ["",""]
console.log(Say.name) // Say