samedi 30 août 2014

Function returning function object rather than prototype value


Vote count:

0




I'm trying to return the value of arr[0], but when I attempt to do just that I end up receiving the test function object. Here is an example:



function test() {
if (!(this instanceof test)) return new test();
console.log(this.arr[0]); //Logs "0"
console.log(typeof this.arr[0]) //Logs "number"
return this.arr[0];
}

test.prototype.arr = [0];

var t = test();
console.log(t); //Logs test function object
console.log(typeof t); //Logs "object"


So, can anyone explain



  1. Why it is returning an object rather than the array value

  2. If there is a way to return the array value


Thanks :)



asked 31 secs ago







Function returning function object rather than prototype value

Aucun commentaire:

Enregistrer un commentaire