不能为我工作了,为什么其中任一不工作:为什么JavaScript无法正常工作?
var Deck = function() {
this.cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
};
var newDeck = new Deck
// console.log(newDeck()); // [1,2,3,4,etc]
console.log(newDeck.cards()); // [1,2,3,4,etc]
returns newDeck.cards is not a function
和
var Deck = function() {
this.cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var cards = function(){
console.log('hey')
}
};
var newDeck = new Deck
// console.log(newDeck()); // [1,2,3,4,etc]
console.log(newDeck.cards()); // [1,2,3,4,etc]
returns the same error as above
我只想一个对象中返回一个函数来自实例
没有函数或方法'cards'。 'cards'是一个有数组的属性。正确的调用将是'console.log(newDeck.cards);' –
@ NinaScholz在第二个例子中有一个卡片功能,虽然?? – hellogoodbye
这只是一个本地函数,而不是实例的一个属性。 –