2

In the fiddle - http://jsfiddle.net/vwwkf18c/ or below code snippet -

var a = [3, 4];
var b = [6, 2];
var c = $.extend({}, a, b);
alert(c[1]); //alerts 2
alert(a); //alerts array a contents
alert(c); //does not return contents of c

My questions - 1) After what has been alerted, we can infer that "c" is an object but not an array object. Please confirm. 2) Secondly it is said that internal representation of an array is an object literal, is that right? Which means that array "a" would be stored as given below -

var a = {
0: 3,
1: 4
}

Is it right? 3) How is a or b stored internally and how is it different from the internal representation of "c"?

maze star
  • 29
  • 1
  • $.extend is a jquery function that expects type Object to be merged together .... – john Smith Jan 09 '15 at 19:22
  • 4
    `alert` is not a debugging tool, if you learn to use the console, it will show things more clearly. `$.extend` creates objects. – adeneo Jan 09 '15 at 19:22
  • In this situation, `c` is now: `Object { 0: 6, 1: 2 }`. That being said, this question isn't really the right fit for SO. Did you have a specific programming question or just theoretical stuff? – Tim Lewis Jan 09 '15 at 19:23
  • Related: http://stackoverflow.com/q/5048371/1883647 – ajp15243 Jan 09 '15 at 19:24
  • as @adeneo said, please debug via `console.log()`, not `alert()`. – Andrew Dunai Jan 09 '15 at 19:24
  • @AndrewDunai Alright, fair enough. – Tim Lewis Jan 09 '15 at 19:24
  • @TimLewis I repeated to emphasise on `console.log` function in case OP doesn't know what does "console logging" mean. – Andrew Dunai Jan 09 '15 at 19:26
  • So, how is an array stored internally, as an object literal? – maze star Jan 09 '15 at 19:26
  • @mazestar That depends on the browser's JavaScript engine implementation, which varies between browsers. – ajp15243 Jan 09 '15 at 19:27
  • arrays can be thought of as akin to objects with a few extra inherited members like length, concat, etc. for example, {0:true,1:false,length:2} can be apply()'ed to most methods that typically use a "real array"; [].filter.call( {0:true,1:false,length:2} , Boolean).length===1 – dandavis Jan 09 '15 at 19:29

2 Answers2

1
  1. See the docs: "Returns: Object"
  2. No. An object literal is a piece of JavaScript syntax for creating objects with. Arrays are an object type that inherits (along the prototype chain) from basic Objects. The Array type has a different toString method than the basic Object, which is why alert gives different results.
  3. That is implementation specific (and also of no importance to anyone writing JavaScript rather than a JavaScript runtime)
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Corollary to #2: JS Arrays are Objects. Arrays have a syntax which allows numerical indexing, which pure Objects don't. The likelihood is that an Array actually has fields arr.0, arr.1, arr.2, etc., but as #3 points out, it's implementation dependent and you shouldn't be in there. Extending Arrays using jQuery is not advisable. – user1329482 Jan 09 '15 at 19:57
0
  1. Yes, c is an object, but not an array.
  2. The internal representation of a (which is an array) has a property called 'length' in addition to '0' and '1'
  3. c has the properties of '0' and '1', but not the property 'length'

Check out this jsfiddle

var a = [3, 4];

var b = [6, 2];

var c = $.extend({}, a, b);

alert(c[1]); //alerts 2
alert(a); //alerts array a contents
alert(Object.getOwnPropertyNames(c)); //does not return contents of c
alert(Object.getOwnPropertyNames(a));

Hope that is helpful

Vikram

Vikram
  • 73
  • 8