`new Array(n)`和`Array(n)`之间的区别是什么?

时间:2016-02-08 06:53:40

标签: javascript arrays

如果它们都调用构造函数“Array”并生成一个对象,会有什么不同?

我知道如果我们创建一些没有this的对象,我们就失去了new

function Animal(name) {this.name = name}
var duck = Animal('duck'); // undefined

但是new Array(n)Array(n)如何运作?

3 个答案:

答案 0 :(得分:2)

没有区别。检查此article

  

您永远不需要在JavaScript中使用新的Object()。使用该对象   文字{}代替。同样,不要使用新的Array(),使用数组   文字[]而不是。 JavaScript中的数组与数组完全不同   在Java中,使用类似Java的语法会让你感到困惑。

     

不要使用新的Number,new String或new Boolean。这些形式产生   不必要的对象包装器。只需使用简单的文字即可。

     

...............................

     

所以规则很简单:我们唯一应该使用new运算符的是   调用伪古典的构造函数。在打电话时   构造函数,新的使用是强制性的。

答案 1 :(得分:1)

vertx.eventBus().consumer("myservice.getdata").handler(msg -> { tdbClient.getConnection(tConresult -> { if (tConresult.succeeded()) { sdbClient.getConnection(sConresult -> { if (sConresult.succeeded()) { SQLConnection tConnection = tConresult.result(); SQLConnection sConnection = sConresult.result(); tConnection.query("select * from t1", t1 -> { if (t1.succeeded()) { tConnection.query("select * from t2", t2 -> { if (t2.succeeded()) { tConnection.query("select * from t3", t3 -> { if (t3.succeeded()) { sConnection.query("select * from s1", s1 -> { if (s1.succeeded()) { sConnection.query("select * from s2", s2 -> { if (s2.succeeded()) { sConnection.query("select * from s3", s3 -> { if (s3.succeeded()) { JsonArray t1Result = new JsonArray(t1.result().getRows()); JsonArray t2Result = new JsonArray(t2.result().getRows()); JsonArray t3Result = new JsonArray(t3.result().getRows()); JsonArray s1Result = new JsonArray(s1.result().getRows()); JsonArray s2Result = new JsonArray(s2.result().getRows()); JsonArray s3Result = new JsonArray(s3.result().getRows()); JsonObject allResult = new JsonObject() .put("t1", t1Result) .put("t2", t2Result) .put("t3", t3Result) .put("s1", s1Result) .put("s2", s2Result) .put("s3", s3Result); msg.reply(allResult); } else {msg.fail(1, "failt to query s3");}}); } else {msg.fail(1, "failt to query s2");}}); } else {msg.fail(1, "failt to query s1");}}); } else {msg.fail(1, "failt to query t3");}}); } else {msg.fail(1, "failt to query t2");}}); } else {msg.fail(1, "failt to query t1");}}); } else {msg.fail(1, "connot get connection to SDB");}}); } else {msg.fail(1, "connot get connection to TDB");}}); }); 的此类行为在规范中有所描述。 您可以像这样执行相同的行为

Array

但更好的想法是遵循一个简单的代码样式约定,所有以captial字母开头的函数都是构造函数,应该使用function Animal(name) { if(!(this instanceof Animal)) { return new Animal(name); } this.name = name } var duck = Animal('duck'); //Animal {name: "duck"} 调用。并设置一个linter,你更喜欢检查你的代码遵循这个规则。

答案 2 :(得分:0)

JavaScript使用原型继承。使用new命令时,它继承自Object。如果您想要从用户定义的对象(例如Animal)继承,您需要使用new Animal()或不使用new,您可以通过以下方式进行操作。

   // Function object acts as a combination of a prototype 
     // to use for the new object and a constructor function to invoke:
     function Animal(name){
      this.name = name
     }

     var inheritFrom = function(parent, prop){
      // This is how you create object as a prototype of another object
      // var x = {} syntax can’t do this as it always set the newly
      //created object’s prototype to Object.prototype.

      var obj = Object.create(parent.prototype);
     // apply() method calls a function with a given this value 
     //and arguments provided as an array 

     parent.apply(obj, [prop]);
     return obj
    }
    var duck = inheritFrom(Animal, 'duck');
    console.log(duck.name);  // ‘duck’