Allen Marshall
Allen Marshall

Reputation: 391

How to use Object create for a prototype?

I'm trying to get Object.create(String.prototype) to function properly. But, for some reason, it never works.

What I have

let s = Object.create(String.prototype);

s.one = function () {console.log("I'm working")};
s.two = function () {console.log("I'm working too")};

a = 'String'

a.one()//Suppose to log I'm working
a.two()//Suppose to log I'm working too



How it's suppose to work

String.prototype.one = function () {console.log("I'm working")};
String.prototype.two = function () {console.log("I'm working too")};

a = 'String'

a.one()//Suppose to log I'm working
a.two()//Suppose to log I'm working too

Upvotes: 0

Views: 69

Answers (3)

user663031
user663031

Reputation:

let s = Object.create(String.prototype);

s.one = function () {console.log("I'm working")};
s.two = function () {console.log("I'm working too")};

a = 'String'

a.one()//Suppose to log I'm working
a.two()//Suppose to log I'm working too

So it would appear you are trying to create an object which has some particular behaviors named one and two. That object will also have the methods of the String prototype available. The problem is, the way you have structured this, there is no way to assign the primitive value to s. Object.create takes a second parameter, but that is for setting specific properties on the created object, not its primitive value, which is impossible.

a has nothing to do with s. Why would you think it did?

If you want to extend the String prototype, then just do that:

String.prototype.one = function() { console.log("I'm working"); };

'whatever'.one();

If you want to extend the String class, then in ES6

class MyString extends String {
  one() { console.log("hi"); }
  two() { console.log("hello"); }
}
  
const myString = new MyString("foobar");
console.log(typeof myString);
console.log("Is myString a sort of string?", myString instanceof String);
myString.one();  

Upvotes: 2

Suren Srapyan
Suren Srapyan

Reputation: 68685

Object.create creates an empty object which's prototype is the given object - String.prototype in your case. So when you add something to the returned result of the Object.create, it adds to the empty object itself, not to the String.prototype. a has no relation with s object.

It the second case you directly add the functions into the String.prototype.

You can see the content of the s object

let s = Object.create(String.prototype);

s.one = function () {console.log("I'm working")};
s.two = function () {console.log("I'm working too")};

console.log(s);

So I think you want to set the prototype of a to the s to have access to one and two functions.

let s = Object.create(String.prototype);

s.one = function () {console.log("I'm working")};
s.two = function () {console.log("I'm working too")};

a = Object.create(s);

a.one()
a.two()

Upvotes: 2

gurvinder372
gurvinder372

Reputation: 68443

But, for some reason, it never works.

You added two properties to s, but a didn't inherited those since you are not using Object.create().

Replace

a = 'String'

with

a = Object.create(s);

let s = Object.create(String.prototype);

s.one = function () {console.log("I'm working")};
s.two = function () {console.log("I'm working too")};

a = Object.create(s);

a.one()//Suppose to log I'm working
a.two()//Suppose to log I'm working too

Upvotes: 3

Related Questions