I have an object method that doesn't work and it's giving me this error:
buyBike: (money) => {
^^^^^^^
SyntaxError: Unexpected identifier
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
This is my code:
let money = 500;
let bike = {
cost: 300
buyBike: (money) => {
if (money >= this.cost) {
money -= this.cost;
} else {
console.log("You don't have enough money to buy this bike.");
}
}
}
So, what is the right way to write an object method in JavaScript?
I've put together the working example so you can see it in code.
let money = 500;
let bike = {
cost: 300,
buyBike: function(money) {
if (money >= this.cost) {
money -= this.cost;
console.log("Sold!");
} else {
console.log("You don't have enough money to buy this bike.");
}
}
}
bike.buyBike(money);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.