Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Math and Array in Javascript?

Javascript seems to take a few liberties when it comes to its built-in types and objects.

To get at the functions inside the Array type you can do this:

> Array().slice
  function slice() {
     [native code]
  }

So here Array looks like a standard function that is being used as a constructor. Except... slice is not a member function of the Array() function, it's a member function of the Array object.

Another unusual thing about Array() is it seems to return an Array object whether you call it with new() or not:

> var a = Array()
undefined
> a
[]
> a.length
0

> var b = new Array()
undefined
> b
[]
> b.length
0

Math on the other hand seems to be a built in singleton object that is always present (ie: no need to instantiate). So you would use Math.min.apply while with Array using Array().slice.apply.

My question is what makes Array() so different than either a constructor you would write yourself and the other built-in objects of Javascript.

like image 827
elasticrat Avatar asked Mar 02 '26 00:03

elasticrat


1 Answers

I'll make some comments inline, quoting your question:

Except... slice is not a member function of the Array() function, it's a member function of the Array object.

More exactly, slice is a member of the Array.prototype object, by executing Array(), you are creating a new array object, that inherits from Array.prototype, that's why slice is available.

Another unusual thing about Array() is it seems to return an Array object whether you call it with new() or not:

This behavior is shared also by other built-in constructors, for example Function:

var fn = Function('return "foo";');

Is equivalent to:

var fn = new Function('return "foo";');

The cases are described in the specification (The Array constructor called as a function vs new Array), although other constructors present a different behavior when you use them with or without new, for example, the primitive wrappers:

new Number("20");        // produces a Number object
typeof new Number("20"); // "object"

Number("20");            // makes type conversion
typeof Number("20");     // "number"

Math on the other hand seems to be a built in singleton object that is always present (ie: no need to instantiate). So you would use Math.min.apply while with Array using Array().slice.apply.

Yes, Math is a simple object, not a function like Array.

It inherits from Object.prototype and IIRC the only peculiar difference that it has with a simple user-defined object is that it's internal [[Class]] property contains "Math", e.g.:

Object.prototype.toString.call(Math); // "[object Math]"

My question is what makes Array() so different than either a constructor you would write yourself and the other built-in objects of Javascript.

Array isn't that different, you could write a constructor that behaves the same way if invoked with new or not, for example:

function Foo (arg) {
  if (!(this instanceof Foo)) { return new Foo(arg); }
  this.foo = arg;
}

new Foo('bar'); // { foo: 'bar' }
Foo('bar');     // { foo: 'bar' }
like image 158
Christian C. Salvadó Avatar answered Mar 04 '26 13:03

Christian C. Salvadó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!