2

I am trying to understand some prototypical concepts of JavaScript which I don't use quite often. Here I have two same methods one for Array another for Function. One works, another not. Could you please explain what is the difference here?

var arr = ['test'];
var string = 'test';

Array.prototype.print = function(){
        console.log(this);
}
Function.prototype.print = function () {
        console.log(this);
}

arr.print(); // logs the arr with value 'test'
string.print(); //logs string.print is not a function
2
  • 4
    Function.prototype.print => String.prototype.print Commented Jun 23, 2017 at 8:44
  • Strings aren't instances of Function so they don't inherit from Function.prototype. ;-) Commented Jun 23, 2017 at 8:49

6 Answers 6

3

You are 'extending' the Function prototype but calling the print function on a String.

Change your code to:

String.prototype.print = function () {
        console.log(this);
}

And it'll work.

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

Comments

2

The error says the problem in your code, that you are not defined the print function on String prototype instead you did on Function which you are not using at all.

String.prototype.print = function () {
//^^^^^--
        console.log(this);
}

var arr = ['test'];
var string = 'test';

Array.prototype.print = function() {
  console.log(this);
}
String.prototype.print = function() {
  console.log(this);
}

arr.print(); // logs the arr with value 'test'
string.print(); //logs string.print is not a function

Comments

1

The first one works because you did it right. You added print function to Array.

The second one doesn't work because you did it wrong. You need to add print function to String:

String.prototype.print = function () {
    console.log(this);
}

Comments

1

string inherit String, you can add print method to String prototype like this:

String.prototype.print = function () {
        console.log(this);
}

Comments

1
[Array,Function,String,Object].forEach(n=>n.prototype.print=function(){
console.log(this);
});

A shortform...

Comments

1

If you are trying to extend a function prototype and accessing the String prototype. You are misunderstanding the prototype inheritance concept

var arr = ['test'];
var string = 'test';
var someMethod = function(){ /* some code */ };

Array.prototype.print = function(){
        console.log(this);
}

String.prototype.print = function () {
        console.log(this);
}

//Extending the prototype of Function
Function.prototype.print = function () {
        console.log(this);
}


arr.print(); // logs the arr with value 'test'
string.print(); //logs string.print is not a function
someMethod.print(); // this will trigger the print method
// extended in Function Prototype.

Update

Very interesting point I realised due this post in Javascript. It is you can treat Function prototype as other prototoypes. Imagine you are extending the functions of a function itself ( seems like one of a kind inception ). So there are interesting methods like call, apply , bind. So we can extend the functionality of even a function. Correct me if I am wrong, Unlike in any other language I can think of, where extending function seems to be impossible. Given that we are not given privilege to touch the source code of that language. Very powerful feature in JS.

1 Comment

@RacilHilan But eventually you are adding a new method into String prototype. Also String is primitive datatype in JS. This print will be available to all variables with datatypes as string. I haven't seen such behaviour in other languages, like extending methods in primitive datatype. Can you please quote an example

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.