【问题标题】:Issue Creating New Object问题创建新对象
【发布时间】:2015-10-14 16:36:24
【问题描述】:
var Contact = function(name, number) {
    this.name = name;
    this.number = number;
}

Contact.prototype.greet = function() {
    document.getElementById("output").innerHTML = this.name + " " + this.number;
};

function createContact(newName, newNumber) {
    var newName = new Contact(newName, newNumber);
}

var tyler = new Contact("Tyler", "555-555-5555");
var mike = new Contact("mike", "555-555-5555");

createContact("steve", "555-555-5555");

alert(steve.name);


$("#addButton").click(function() {
    steve.greet();
});

我对上述代码有疑问。我遇到的问题是 createContact 函数。我想要完成的是编写一个函数,该函数最终将基于用户输入创建一个带有姓名和号码的新联系人。我还希望新联系人对象的名称是 newName 的名称,因此 var newName = new Contact。使用现在的 createContact 函数并不能实现这一点。我可以像使用“tyler”和“mike”联系人一样直接创建一个新的联系人对象,但不能通过函数。

我试图自己找到答案,但没有运气。我真的不知道此时我应该搜索什么,所以我只是卡住了。

另外,当我说我“创建了 Contact 对象的 tyler 对象”时,我是不是用错了“对象”这个词?

谢谢!

【问题讨论】:

  • 您永远不会像使用tylermike 那样创建steve 变量来保存联系人
  • var newName 是一个局部变量而不是全局变量。
  • 只需返回在createContact 中创建的变量.. 请看这个问题stackoverflow.com/questions/5887386/… .. Custom Object 部分

标签: javascript object


【解决方案1】:

这将是一个非常不寻常的功能。但是,您可以通过拥有一个所有这些都将存在的对象来做到这一点(如果您真的希望它可以是全局对象,但我不推荐它),那么:

function createContact(newName, newNumber) {
    theObject[newName] = new Contact(newName, newNumber);
}

例如:

var contacts = {};

function createContact(newName, newNumber) {
    contacts[newName] = new Contact(newName, newNumber);
}

createContact("steve", "555-555-5555");
alert(contacts.steve.name);

或者使用全局变量,通过 window 全局变量:

function createContact(newName, newNumber) {
    window[newName] = new Contact(newName, newNumber);
}

createContact("steve", "555-555-5555");
alert(steve.name);

不过,我还是不鼓励您使用全局变量。全局命名空间已经非常拥挤。


当然,这又是一个非常不寻常的功能。正常的做法是自己做:

var steve = createContact("steve", "555-555-5555");

甚至

var steve = new Contact("steve", "555-555-5555");

但是你说你知道怎么做,所以......

【讨论】:

  • 我还在学习 JavaScript。有没有更好的方法来做我想做的事情?
  • @TylerHines 为什么要将变量命名为函数中的参数?更好的方法就是不这样做。
  • @TylerHines: var steve = new Contact("steve", "555-555-5555"); 是正常的做法。我可能会称之为“更好”,但这是一种意见......
  • 理想情况下,我想做的是根据用户输入创建这些,所以我不知道联系人的名字是什么。可以把它想象成一个普通的通讯录,有人会在其中输入姓名,然后将联系人添加到通讯录中。
  • @TylerHines 但那将存储在variable.name 中,您不需要让variable 部分与name 相同。对于用户而言,如何命名变量并不重要。
【解决方案2】:

createContact 方法中返回实例怎么样。

var Contact = function(name, number) {
  this.name = name;
  this.number = number;
}

Contact.prototype.greet = function() {
  document.getElementById("output").innerHTML = this.name + " " + this.number;
};

function createContact(newName, newNumber) {
  return new Contact(newName, newNumber); // I return the instance here
}

var tyler = new Contact("Tyler", "555-555-5555");
var mike = new Contact("mike", "555-555-5555");

var steve = createContact("steve", "555-555-5555");

alert(steve.name);

【讨论】:

  • 我认为 OP 试图用 createContact() 函数省略 var steve = 部分。
【解决方案3】:

一个解决方案是用这个修改:

function createContact(newName, newNumber, context) {
    var ctx = context || this;
    ctx[newName] = new Contact(newName, newNumber);
    return ctx[newName];
}

var Contact = function(name, number) {
    this.name = name;
    this.number = number;
}

function createContact(newName, newNumber, context) {
  var ctx = context || this;
  ctx[newName] = new Contact(newName, newNumber);
  return ctx[newName];
}

createContact("Tyler" , "555-555-5555");
createContact("mike"  , "555-555-5555");
createContact("steve" , "555-555-5555");

alert( Tyler.name + ' | ' + mike.name + ' | ' + steve.name );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多