I was experimenting with a legacy in JavaScript, and wrote two functions:
  Object Prototype.inherits = function (obj) {this.prototype = new obj;} Object.prototype.pass = function (obj) {obj.prototype = new this;}   This code works very well:
  Dog.inherits (animal);   but the following fails:
  Animal.pass (dog);   As I think, I do not work, because "this" is not a reference to the object instance? If this is the case, how can I refine the object from within me?
Thank you in advance!
OK, in fact these two are doing exactly the same:
< Code> Dog.prototype = new animal;
 Under the methods  this  value will refer to  base object , where reference was applied: 
 < Code> dog inheritance (animal);     This  value  dog  will reference the constructor function, and  obj  will be the argument  the animal  function 
  When you call: 
   Animal.pass (dog);     This will reference the  animal  function, which in the end does the same thing, as  inherits  Method, but on the other side. 
  I recommend you to  not  expand the  Object.prototype  object, because it can give you a lot of problems, for example For those, both of those properties will be enumerated in any  in the  loop, for example: 
  for  (var prop in {}) {// & Lt; - An empty object! Warning (support); // 'Inheritance' and 'Pass'}  
  All objects are from  Object.prototype , and it seems that you use these methods Only Function Objects, it will be safe to extend the  Function.prototype  object, or to implement the methods as tasks that carry two parameters. 
 
Comments
Post a Comment