简体   繁体   English

如何将对象创建用于原型?

[英]How to use Object create for a prototype?

I'm trying to get Object.create(String.prototype) to function properly. 我正在尝试使Object.create(String.prototype)正常运行。 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 

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() . 您向s添加了两个属性,但是a没有继承那些属性,因为您没有使用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 

Object.create creates an empty object which's prototype is the given object - String.prototype in your case. Object.create创建一个空对象 ,该对象的原型是给定的对象-在您的情况下为String.prototype 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 . 因此,当您将某些内容添加到Object.create的返回结果中时,它将添加到空对象本身,而不是String.prototype a has no relation with s object. as对象无关。

It the second case you directly add the functions into the String.prototype . 在第二种情况下,您可以直接将函数添加到String.prototype

You can see the content of the s object 您可以看到s对象的内容

 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. 所以我觉得你要的原型设定as有机会获得onetwo功能。

 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() 

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 . 因此,您似乎正在尝试创建一个对象,该对象具有一些名为“ one和“ two特定行为。 That object will also have the methods of the String prototype available. 该对象还将具有String原型的方法。 The problem is, the way you have structured this, there is no way to assign the primitive value to s . 问题是,以这种方式构造该结构,无法将原始值分配给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. Object.create需要第二个参数,但这是为了在创建的对象上设置特定属性,而不是其原始值,这是不可能的。

a has nothing to do with s . as无关。 Why would you think it did? 您为什么会认为呢?

If you want to extend the String prototype, then just do that: 如果要扩展String原型,则只需执行以下操作:

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

If you want to extend the String class, then in ES6 如果要扩展String类,则在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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM