0

I have a custom string prototype that does some actions to a string;

String.prototype.norm_to_ascii=function(){return unescape(encodeURIComponent(this))};

In my example, the string that I want to apply the prototype to is a global object property that lives outside of the SampleObject object. In my actual code it would be referenced like this;

var userObject = {
   name: "SomeName",
   id: "SomeID"
}

It works everywhere in my project (other js files) except for within a particular Object method;

var SampleObject = {   //This is in it's own js file called sampleobject.js
   test: 0,
   doStringThings {
      let something = userObject.id.norm_to_ascii()  //RETURNS userObject.id.norm_to_ascii is not a function
   }
}

So in the SampleObject, I need to use the id, for example, but I need to do some basic decoding of the id value that is in the userObject which is what the string prototype does.

I can use this string prototype elsewhere. This is in a chrome extension so I have defined the prototype in the service worker and it can be used in the popup and content pages as well as the service worker so it must have to do with the object method but I can't figure out why? Can anyone offer any suggestions to expose that prototype to the object method without having to redefine it?

EDIT I should have been more clear in my explanation. I updated my example above.

3
  • You have to make sure that your code that creates the String prototype method runs before the other code runs. Also, you should use Object.defineProperty() to set up the new method, for various technical reasons. Commented Jun 30, 2022 at 17:44
  • You should edit your question and include a clear description of exactly where you perform that change to the String prototype, and how that context relates to the contexts where things don't work. Commented Jun 30, 2022 at 17:47
  • @Pointy The String prototype is defined at the beginning of the service worker so it is definitely loaded before i try to use it in the method. The Object already exists and is in use, I am just making some changes which requires the need to add the string prototype function to the method. Commented Jun 30, 2022 at 18:25

2 Answers 2

1

You forget about this

this.otherTestValue.norm_to_ascii()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply but that doesn't apply here. My bad for not explaining better. I have updated the question.
0

After seeing the updated question my conclusion is that you are defining the norm_to_ascii function after you run it.
Changing the order of the imports should fix the problem. Can you show us the structure of the project and where are you importing the file with that prototype?

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.