简体   繁体   中英

Creating objects in JS

I am trying to create objects in JS for the first time (for a game engine), but I am not able to establish the properties. I have the following code:

function door(x, y, goto, key) {
    this.x = x;
    this.y = y;
    this.goto = goto;
    this.key = key;
}

But, when I call the function

var door1 = door(1,2,3,4);

It says 'x' does not exist. I have done some experiments changing variables names, and it refers to 'this.x', not the xi pass as a parameter. Does anyone know how can I set the value?

您忘记了新关键字...

var door1 = new door(1,2,3,4);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM