0

I use string.prototype to linkify text passages on my website. On some sites I want to add different notes and thus want to pass an additional argument to linkify.

My initial idea was to do it as follows.

function linkifyText() {
    
    var uglyLinksPattern = /\[(.*?)\]/gim; // match all letters between square brackets
                
    if(!String.linkify) {
        String.prototype.linkify = function(note) {
            var textInput = this;    
            return textInput.replace(uglyLinksPattern, '<a target="_blank" rel="noopener nofollow" href="$&">$&</a>' + note);            
        }
    }
    return uglyLinksPattern
}
        
function linkifyDialogue (text) {    
    linkifyText();
    var note = 'Ad';        
    var linkedText = String.linkify.call(text, note);
    $('#textElem').html(linkedText);        
}

I found some tutorials using call and apply. However, I wasn't able to transfer it to my case and hope to get an answer on how to pass an argument to the string.prototype property. So what is the trick?

8
  • 1
    Do you want to define String.linkify (static method) or String.prototype.linkify? Commented Nov 7, 2020 at 22:12
  • String.prototype.linkify Commented Nov 7, 2020 at 22:13
  • 1
    You're testing for the wrong thing in that if then. Commented Nov 7, 2020 at 22:14
  • Not sure why you're capturing var textInput = this when you could just use this.replace instead. Also given that it's 2020 why not const uglyLinksPattern? Try and use let and const instead of var. Commented Nov 7, 2020 at 22:15
  • Okay, you have a point there. textInput was for better readability. Commented Nov 7, 2020 at 22:20

1 Answer 1

1

The way you've tried to implement it is kinda weird (no offense intended). You could've made this much simpler. See:

//Do this just once
String.prototype.linkify=function(note) {
    return this.replace(/\[(.*?)\]/gim,"<a target='_blank' rel='noopener nofollow' href='$1'>$1</a>"+note);
};

function linkifyDialogue(text) {    
    var note="Ad",        
        linkedText=text.linkify(note);
    $('#textElem').html(linkedText);        
}

All strings are objects already. If you add a method to the prototype, there's no need to use call() or apply() unless you actually need to (i.e. to call it with a different value for this, pass an array of values as different parameters, etc.).

Sign up to request clarification or add additional context in comments.

3 Comments

thx. so this is the very simple form, of the entire function. There are email-address, pseudo patterns, markdown-patterns etc in it. Some have escaped text, some not... The string prototype is defined in a constructor and has to be called across different instances... So, it grew like this and looks a bit overblown in my posting. However, if I want to call string.prototype in linkify inside another function, how can I pass the variable into it?
You define with prototype, you don't call with it.
There's only one string prototype. Every change you make is immediately available in the whole window. So if you want to call it from somewhere else, it will be available. And it wouldn't make sense to redefine linkify at different parts of your code. If you need it to have different behaviors based on the context, you could: a) add another parameter to tell it what to do (String.prototype.linkify=function(note,somethingElse,whatever)), or b) don't use String.prototype at all and create a class to contain all the different text processing methods.

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.