views:

25

answers:

2

Just wondering what are the side effects or what does this mean or do. I noticed it in two parts of the source code for the Prototype JS library.

Hash.from = $H;

Array.from = $A;

any clarification would be greatly appreciated, and Thank you in advance.

A: 

From the docs

Array.from clones an existing array or creates a new one from an array-like collection. This is an alias for the $A() method.

$A() is a convenience alias of Array.from, but is the preferred way of casting to an Array.

That's what is happening in the lines you posted. The named function $A is being assigned to Array.from. So calling Array.from(iterable) is the same as calling $A(iterable). Same with Hash.

Satyajit
A: 

It's hard to give a useful answer without more context.

Hash.from = $H;

Assigns value of variable $H to property from of object Hash. This is all user-defined stuff.

Array.from = $A;

Assigns value of variable $A to property from of object Array. This is all user-defined except Array, which is a built-in constructor for arrays.

The side effect of modifying built-in constructor functions is the possibility that two sets of modifications will conflict with each other. For example, if prototype.js gives Array a from property and then myCoolLibrary.js also gives Array a from property, probably with a different API, one library or the other is bound to break.

no