Goodnight. Faced a problem. There is approximately the following construction:

/** * Создаем объект класса, пихаем в его прототип свойства */ var ParentClass = new Function; var pContainer = ParentClass.prototype; pContainer[<prop_name>] = <prop_value> /** * Создаем дитё, задача - пихнуть в прототип cContainer-а свойства класса- родителя, но так, чтобы при изменении свойства прототипа родителя в ребенке - в прототипе родителя значения не менялись */ var ChildClass = new Function; var cContainer = ChildClass.prototype; // ДОлжно было прокатить .... не прокатило :( Extends(cContainer, ParentClass); function Extends( child, parent ) { var cProto = child.__proto__; var pProto = parent.prototype; var Ext = new Function; Ext.prototype = pProto; Ext.constructor(); cProto = Ext.prototype; } 

Please help with Extends feature: D

PS Please do not laugh with the Extends function. As I just did not try: and eval th, and than just not tried. The main thing is that the ChildClass object is an instance from the rotor.

Thank you in advance

  • one
    Search use :) tyts - Zowie
  • Thank you, try to rake up: D - Stanislav Komar
  • And I really like the inherits from backbone.js - Specter
  • @Spectre - but I don’t really like backbone.js; D - Zowie
  • justify that exactly? philosophy? implementation? code style? and in more detail, but I would also like to hear an alternative in the niche occupied by the subject - Specter

1 answer 1

In JS, non-simple variables are copied by reference. Those. by value, only numbers, strings, bool, etc. are copied, but objects, arrays, and functions are copied by reference. in the underscore library there are a bunch of methods for working with arrays and objects, among which is .clone () which returns a new object with exactly the same set of simple properties, copying them by value. To copy the entire object completely, you need to write something like this: (showing with an example of one model from my project)

 profile.prototype.clone = function(){ var q = new profile(); for(var i in this){ if(typeof this[i] == 'function') q[i] = this[i]; else q[i] = _.clone(this[i]); } return q; } 

those. then I go through all the properties of my object and if it is a function, then I copy it to a new one, and if I’m a different value, then I give the Underscore library for cloning.


If you just need to take and make an object the parent instance, then perhaps this example will help with http://javascript.ru/tutorial/object/inheritance

 function extend(Child, Parent) { var F = function(){ }; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.superclass = Parent.prototype; } 
  • > .clone () which returns a new object with exactly the same set of simple properties, copying them by value. To copy the entire object, you need to write something like this. What? > Create a shallow-copied clone of the object. Any nested objects or arrays will be copied by reference, not duplicated. .clone (object) var a = _.clone (); // equivalent to var a = _.extend ({}, _); a.identity // function (value) {return value; } and why is your clone better? - Specter
  • _.clone () is not equivalent to _.extend (). I did not understand the question at the end. Maybe I did not understand the question of the author. - MuFF
  • to copy an object, your wrapper over _.clone superfluous, and the function itself is sufficient. > .clone () is not equivalent to .extend (). Take a look at the source code _.clone and make sure that in the case of copying an object - equivalent - Specter
  • In my case, it is not superfluous, since the methods of my object when cloned as follows return _.clone (this); they will be turned into empty objects, plus it was necessary for me that the clone method be on my object, and not to transfer my object to an external function. In general, the clone and extend methods are not equivalent. In your example, when an empty object is submitted, the return result is the same. In the source clone, this particular case is used. And the extend function returns a new object, but if the properties of the ancestor object are an object, then they will be copied by reference. - MuFF 2:07 pm