I am new to jquery and basically, I was just looking at the source of the the get() function in jquery and it looks something like below (Jquery 11.1.3) :
get: function( num ) {
return num != null ?
// Return just the one element from the set
( num < 0 ? this[ num + this.length ] : this[ num ] ) :
// Return all the elements in a clean array
slice.call( this );
},
Now I understand most part of this function, I have used my basic understanding of js to make sense of what this function is doing and heres what I think, the return statement is a ternary operator that will check if num is a negative or a positive number (i.e. num < 0) , and will conclude to a given condition,
If num is a negative number this[ num + this.length ] is returned and if not
this[ num ]
will be returned , ok , fair enough , now what is the part of :
slice.call( this );
doing in this function ?
I assume it is converting a object to a array incase you pass a object instead of a pure array, I made the following function to prove my point to myself :
function get(elem) {
return Array.prototype.slice.call( elem );
}
obj = {
'name' : 'putin',
'sname' : 'valdimar'
}
var str = get(obj);
console.log(str); // returns empty array.
As you can see str is an empty array instead of being a object that is converted to a pure array. So I guess I am missing something very trivial, but can somebody now elaborate and make sense of what is slice.call(this) doing in the get() method in jquery?