How does Prototype extend objects?

I read the Prototype source code while learning Javascript. I was wondering where is the code that is used to extend our own objects.

I have seen,

Object.extend(Function.prototype, (function() {
Object.extend(String.prototype, (function() {
Object.extend(Number.prototype, (function() {

all over the place and I can't find where the .extend function comes from.

I have seen that:

  function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
  }

in line 194-198 and I wonder if this is the one. I can not understand how this happens, if so.

In any case, my question, as I said above, is how / where Prototype extends its own objects.

+3
source share
2 answers

Yes, this is the function that you see later in the code that you will see to get Object.extend, for example:

extend(Object, {
    extend: extend, //here where the magic gets added
    inspect: inspect,
    toJSON: NATIVE_JSON_STRINGIFY_SUPPORT ? stringify : toJSON,
    toQueryString: toQueryString,
    toHTML: toHTML,
    keys: Object.keys || keys,
    values: values,
    clone: clone,
    isElement: isElement,
    isArray: isArray,
    isHash: isHash,
    isFunction: isFunction,
    isString: isString,
    isNumber: isNumber,
    isDate: isDate,
    isUndefined: isUndefined
});

, extend() Object, .extend Object.

+3

Source: https://habr.com/ru/post/1780064/


All Articles