0

I need to understand how the Array object gets a reference to Object.

For example, when I create var arr = []; it has Array.prototype ---> Object.prototype ---> null.

I want to achieve above for the example below:

Suppose I have a function xyz(), where xyz.prototype.somefunction = function() { }.

I have another function abc(), where abc.prototype.anotherfunction = function() { }.

When I create an object of abc() — as in var obj = new abc() — I want it to have a prototype chain like obj ---> abc.prototype ---> xyz.prototype ---> object.prototype ---> null.

Please suggest the best way to do this.

5
  • 1
    Object.setPrototypeOf(abc.prototype, xyz.prototype); Commented Jun 16, 2017 at 16:41
  • Please use backticks to format code properly. Commented Jun 16, 2017 at 16:47
  • What are you trying to achieve? Or is this some kind of academic learning exercise? Commented Jun 16, 2017 at 16:49
  • Its for some learning purpose only. Thanks for the answer. Commented Jun 16, 2017 at 16:54
  • @torazaburo is it possible when i create an object of abc same time its does what mentioned above. Right now i need to add the Object.setPrototyeof() se Commented Jun 16, 2017 at 17:13

2 Answers 2

1
function xyz() {}
function abc() {}
var p = new xyz();
abc.prototype = p;

var o = new abc();
o.__proto__ === p // true
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true

Or:

function xyz() {}
function abc() {}
var p = Object.create(xyz.prototype);
abc.prototype = p;    

var o = new abc();
o.__proto__ === p // true
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true

Or:

class xyz {}
class abc extends xyz {}

var o = new abc();
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true

Or:

function xyz () {}
const abc = {
    __proto__: Object.create(xyz.prototype)
}

abc.__proto__.__proto__ === xyz.prototype // true
abc.__proto__.__proto__.__proto__ === Object.prototype // true
abc.__proto__.__proto__.__proto__.__proto__ === null // true

Or:

function xyz () {}
function abc() {}
Object.setPrototypeOf(abc.prototype, xyz.prototype);

var o = new abc();
o.__proto__.__proto__ === xyz.prototype // true
o.__proto__.__proto__.__proto__ === Object.prototype // true
o.__proto__.__proto__.__proto__.__proto__ === null // true
Sign up to request clarification or add additional context in comments.

2 Comments

The one with const abc = { isn't declaring a function like the rest.
Great. This what i was looking for .. :)
0

You can do prototypical inheritance:

abc.prototype = new xyz();

now, when you create a new object, it will follow the chain you desire.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.