我有一个对象构造函数,例如:

function myObjConstr () {
    console.log(object_name);
}


我想要这个结果:

var name1 = new myObjConstr(); //print in console log "name1"
var nameff = new myObjConstr(); //print in console log "nameff"

最佳答案

您需要将对象名称传递给构造函数:

function myObjConstr (obj_name) {
    this.object_name = obj_name;
    console.log(this.object_name);
}

var name1 = new myObjConstr("name1");
var nameff = new myObjConstr("nameff");

09-08 11:38