Jump to content

objectSetPrototype usage


jimfog
 Share

Recommended Posts

I am trying to use objectSetPrototype. here is the code w  2 classes

var A = /** @class */ (function () {
    function A() {
    this.name='6';
    }
    A.prototype.test=function()
    {
    return 'john';
    };
    return A;
}());

var B = /** @class */ (function (_super) {
    function B() {
        return _super !== null && _super.apply(this, arguments) || this;

    }
    B.prototype.othertest=function(){return 'Anna'};
    
    return B;
}(A));

Object.setPrototypeOf(B,A);

var testInheritance=new B();
console.log(testInheritance.test());

nonetheless testInheritance.test() does not print in the console "john" as it should...no inheritance taking place.

I get the following mesaage in the console:

Uncaught TypeError: testInheritance.test is not a function

Furthermore:It is interesrting to see what B.prototype prints in the console

https://1drv.ms/u/s!AjCBrCpLQye-ihUHb_9Ta5FLkDXp?e=FiIsAn

Take a look at the red arrow it points to the [[prototype]] and confirms that f A() was set as prototype...nonetheless as already said inheritance is not taking place

Cannot call test() which is a method f A-after instantiating B of course.

 

Link to comment
Share on other sites

Checking MDN, the second argument of setPrototypeOf() has to be an actual prototype. To make it work, your code just needs to change to this:

Object.setPrototypeOf(B, A.prototype);

 

Link to comment
Share on other sites

well...object.setPrototypeof seems to work only w objects...but.

I do not know if you are familiar with the extends pollyfill(it simulates class inheritance)....ES6 made it native https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends

One post about _extends pollyfill is here https://stackoverflow.com/questions/45954157/understanding-the-extends-function-generated-by-typescript 

Go where it says about Note 2.

By reading it you will reach the conclusion that setPrototypeof accepts constructor functions as arguments which does not make sense...since as you said and according to mdn accepts only objects.

WHat do you think?

 

Link to comment
Share on other sites

I did not work much with inheritance in old Javascript because it was unnecessarily complicated and I did not need it that badly. Fortunately, now you can just use the class and extends keywords so I would recommend using that.

It seems I wasn't correct in my previous post. It looks like changing A to A.prototype is not the solution, A itself works but there is one additional step needed to properly inherit a class in old Javascript. The prototype of B has to be assigned an instance of a copy of A. In the code shown in stackoverflow, they named the copy "__()" and they set the value of its constructor to A's constructor. If you do the same, your original code works:

Object.setPrototypeOf(B,A);

// Assign an instance of a copy of A() to B's prototype
(function() {

  function __() { this.constructor = A; }
  __.prototype = A.prototype;

  B.prototype = new __();

})();

I now remember reading about using a dummy class for inheritance a long time ago, but I never end up memorizing how to do it. In the above code "__()" is the dummy class, I've wrapped it in an anonymous function so that it doesn't occupy the global scope.

Link to comment
Share on other sites

I think yours answer explains things very good...there is one last point that I would like to be clarified cause it creates confusion to me.

So inheritance in old JS is achieved with these 2 steps...

If there is sth I do not understand is why to use setObjectPrototype in th first place...after testing the code it seems clear that the "bulk" of the work is done from the second part..(dymmy constructor etc).

I just want some last comments on this...THX.

Link to comment
Share on other sites

Unfortunately, I don't fully understand it because I never put the time into researching it. It wasn't worth the trouble for me.

It does seem kind of redundant to have to call setObjectPrototype() and then later manually assign the prototype. Perhaps you could try to search on Google for a clear explanation on how this technique works and why.

Link to comment
Share on other sites

On 11/13/2021 at 8:40 AM, Ingolme said:

Unfortunately, I don't fully understand it because I never put the time into researching it. It wasn't worth the trouble for me.

It does seem kind of redundant to have to call setObjectPrototype() and then later manually assign the prototype. Perhaps you could try to search on Google for a clear explanation on how this technique works and why.

Yes it seems redundant...but as you imply there is no point investigating further...since modern JS provides native inheritance functionality.

And with the above our discussion is cncluded..thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...