1

This is a javascript function:

String.prototype.digit = function() {
  console.log(this); // 'this' contain the object 
  return false;
};

How can I access the argument '14' in function while calling function like this:

'14'.digit();
1
  • FYI: A proper function call would be like function(arg) { console.log(arg); } The keyword this is not the argument but the object you are calling the function from. Commented Feb 20, 2017 at 12:48

1 Answer 1

5

You could use Object#valueOf method

The valueOf() method returns the primitive value of the specified object.

String.prototype.digit = function() {
    console.log(this.valueOf());
    return false;
};

'14'.digit();

or Object#toString method.

The toString() method returns a string representing the object.

String.prototype.digit = function() {
    console.log(this.toString());
    return false;
};

'14'.digit();

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

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.