【问题标题】:My creation of a simple object is not working correctly?我创建的简单对象无法正常工作?
【发布时间】:2014-04-01 15:44:51
【问题描述】:

我想创建一个简单的对象,然后使用构造函数填充该对象,就像在 OOP 语言中一样。为什么 Javascript 不允许我创建基本对象

person = new Object();

问题:在 Javascript 中声明对象以使其基本上遵循与 Java 和 C++ 相同的约定的最佳方式是什么?

我希望能够在代码中使用对象属性之前添加它们。

代码:

<!DOCTYPE html>
<html>
<body>

<script>

person=new Object(); <-- will work with out this code

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

myFather=new person("John","Doe",50,"blue");

document.write(myFather.firstname + " -- " + myFather.age);
</script>

</body>
</html>

【问题讨论】:

  • 请注意,JavaScript 一种 OOP 语言。它只是基于原型而不是基于类。
  • 我不明白这个问题。构造函数有什么问题?
  • 您的代码没有意义。首先你声明一个函数(person,注意,JS 中的函数被提升了),然后你用一个空对象覆盖它......
  • 在 Javascript 中声明对象的最佳方式是什么,以便它基本上遵循与 Java 和 C++ 相同的约定[原文如此]?:不同的语言,不同的约定。不要试图让该语言与其他语言一样工作,否则你会很长时间感到沮丧。

标签: javascript


【解决方案1】:
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

myFather=new person("John","Doe",50,"blue");

document.body.innerHTML=myFather.firstname + " -- " + myFather.age;

有效,您不需要person=new Object(),因为无论如何您都在函数语句中定义人员。在 javascript 中,函数也是对象构造函数。这就是为什么你可以在函数上调用new

http://jsfiddle.net/camus/bBj8f/

问题:在 Javascript,因此它基本上遵循与 Java 相同的转换 和 C++?

Javascript 不必遵循 Java 或 C++“约定”,Javascript 不像 Java 或 C++ 那样工作。

【讨论】:

  • 我打算自己发布这个,但是 OP 在他的问题中说明了这一点,我不知道他在问什么。
【解决方案2】:

你的线路

person=new Object();

这里不需要。只需删除该行,您的代码就可以工作:http://jsfiddle.net/s5UDq/

【讨论】:

    【解决方案3】:

    只是对您的代码的一小部分剖析:

    在这里,您正在创建一个(全局变量),其中包含一个对象。

    person=new Object();
    

    下一行您正在创建一个名为person 的新函数:注意这不是以函数为值的变量。

    function person(firstname,lastname,age,eyecolor){
      this.firstname=firstname;
      this.lastname=lastname;
      this.age=age;
      this.eyecolor=eyecolor;
    }
    

    之后,您将使用命名函数作为对象 person 的隐式构造函数。

    解决方案是创建一个变量 person,将函数作为值,或者只创建命名函数。

    对于前者,看看这个:

      var person = function(firstname,lastname,age,eyecolor){
        this.firstname=firstname;
        this.lastname=lastname;
        this.age=age;
        this.eyecolor=eyecolor;
      }
    

    对于后者,只需删除 person = new Object(); 行。

    【讨论】:

      【解决方案4】:

      解决办法:
      删除person = new Object(); 行,然后您的代码将按预期工作。

      原因:
      您的代码中出现错误的原因是由于 javascript 中的 函数提升(请参阅 http://designpepper.com/blog/drips/variable-and-function-hoisting)。因此,首先定义了函数player。之后,您使用 emtpy 对象(使用 person = new Object(); 行)覆盖函数 player

      所以这就是为什么会出现这样的错误:Uncaught TypeError: object is not a function

      看看额外的 cmets:

      console.log(person); // logs function person(firstname,lastname,age,eyecolor) ...
      
      person=new Object(); // here you overwrite your function
      
      function person(firstname,lastname,age,eyecolor) // at this point the function is already declared
      {
      this.firstname=firstname;
      this.lastname=lastname;
      this.age=age;
      this.eyecolor=eyecolor;
      }
      
      console.log(person); // logs Object {}
      
      myFather=new person("John","Doe",50,"blue"); // Uncaught TypeError: object is not a function
      
      console.log(myFather.firstname + " -- " + myFather.age);
      

      【讨论】:

      • 如果我在谷歌浏览器中打开一个页面,它会显示页面上的所有错误吗?
      • 不,你可以在javascript控制台看到错误(你可以用F12键进入控制台)
      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多