[英]why when I console.log(event) for `from` onSubmit, I don't see that preventDefault method
[英]Why i don't get the console.log in JS
環境中的新人。 我制作了一個 Object 構造函數來制造汽車:這是我的代碼:
function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } var $1stCar = Car("Ferrari", "Enzo", 2015); var $2ndCar = Car("Porsche", "911", 2018); var $3rdCar = Car("Lamborghini", "Huracan", 2019); var $4rdCar = Car("Mono", "Bach", 2020); var $5thCar = Car("Bugatti", "Chiron Super Sport 300", 2022); var $6thCar = Car("Bugatti", "SuperBuq - 1st", 2025); var $7thCar = Car("Tesla", "Rod-X", 2035); console.log($1stCar);
我得到“未定義”。 對我來說,這個變量是有效的,因為它的開頭有一個美元符號。 任何人有提示? :)
快樂編碼!
您忘記了new
運算符來創建實例。
function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } let $1stCar = new Car("Ferrari", "Enzo", 2015); console.log($1stCar);
您需要在使用構造函數之前添加新的。
function Car (make, model, year) { this.make = make; this.model = model; this.year = year; } let $1stCar = new Car ("Ferrari", "Enzo", 2015); console.log ($1stCar);
問:為什么顯示未定義?
常規與構造函數 Function 的區別
常規 Function:
function example(value){
return value
}
調用常規 Function 並將返回值存儲到變量中:
let store = example("Hello!")
構造函數 Function:
function Example(name){
this.name = name
}
使用新的運算符調用構造函數 Function:
let Store = new Example("Hello")
// 這將返回一個新的 object 並將新創建的 object 分配給 Store 變量。
當您使用構造函數 function 創建對象時,您需要使用新的運算符調用它,以便它返回 object。
使用“新”關鍵字:
let firstCar = new Car ("Ferrari", "Enzo", 2015);
代替
var $1stCar = Car ("Ferrari", "Enzo", 2015);
並且 $ 作為變量前綴通常與 PHP 一起使用,但在 JS 中不使用 - 它可以工作,但並不常見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.