1

I am learning and trying to understand JavaScript Objects deeply. I learnt Object prototype via example below.

var MyObj = function(){
   this.Name = "Stackoverflow";
}

MyObj.prototype.prop = "OK"; 

var instance = new MyObj();
instance.prop = "OK";

console.log(instance.hasOwnProperty("Name")); //true
console.log(Object.getOwnPropertyNames(instance)); //[ 'Name', 'prop' ]

In the example above, instance object can not access getOwnPropertyNames of Object. Because getOwnPropertyNames function is not a member of prototype of Object. Following this, when i write my own object like Object above e.g.

var MyDream = function(){}
MyDream.prototype.canAccessThisMethod = function(x){};

var instanceSample = new MyDream();

instanceSample.canAccessThisMethod("blabla"); //because of prototype
MyDream.method(blabla); // didn't work.

How to make MyDream.method work in this example?

Rohit416
  • 3,416
  • 3
  • 24
  • 41
  • 1
    Welcome to StackOverflow. First of all few things, You want to learn `prototypes` which is good, anyway it does not have anything to with adding a method to the `function` here. But you will get around these things on the way. – Rohit416 Jun 29 '16 at 11:43
  • Here is a [nice tutorial on JavaScript prototypes.](https://www.pluralsight.com/blog/software-development/understanding-javascript-prototypes) – Rohit416 Jun 29 '16 at 11:44

2 Answers2

1

You can do this, for example:

instanceSample.prototype.method = function( x ){
...
}

Prototype means that the method "method" will be added to all objects of the class "MyDream"

Thiago Souza
  • 1,093
  • 3
  • 13
  • 31
1

Apart from explaining the prototypes, let us focus on the thing you asked in following snippet i.e.

var MyDream = function(){}
MyDream.prototype.canAccessThisMethod = function(x){ };

var instanceSample = new MyDream();

instanceSample.canAccessThisMethod("blabla"); //because of prototype
MyDream.method(blabla); //How can we write "MyDream.method" in this example

Let us go step by step.

var MyDream = function(){}

You created an anonymous function and assigned it to MyDream variable. Functions are first class objects in JavaScript. They are powerful and flexible. So being a first class object you can assign them to variables like you did. You can pass them as parameters. On top of that you can also extend there prototype chain as

MyDream.prototype.canAccessThisMethod = function(x){ };

I believe you have figured out the rest of the stuff so skipping the details and on fast forward moving to this statement.

MyDream.method(blabla);

Unfortunately, this will not work. The reason being, to add a function as method you need to assign it as function literal. You can add methods to function just like Objects.

MyDream.method = function(x) {
    console.log(x);
}

MyDream.method("bla bla");
Community
  • 1
  • 1
Rohit416
  • 3,416
  • 3
  • 24
  • 41
  • Hi Rohit416. Thanks for reply. You said that "MyDream.method" will not work. It works only as a literal function. I understand here no problem. I wonder how Object.getOwnPropertyNames works ? –  Jun 29 '16 at 12:45
  • Is it possible to write a function which has inaccessible methods from inherited objects as a literal function –  Jun 29 '16 at 12:52
  • _"I wonder how Object.getOwnPropertyNames works"_ It has a different purpose, it returns all the property names and methods which are present in the object. Also, all built in types are treated as an instance of Object. A `function` prototype is actually an `Object` which ultimately points to `function( ){ }`. – Rohit416 Jun 30 '16 at 06:48
  • _"Is it possible to write a function which has inaccessible methods from inherited objects as a literal function"_ Technically, if you have a function containing few methods, not on its prototype chain then it is possible to decide which method to expose but once they are on prototype chain of that function then you can-not hide those methods from inherited object. You can take an example of any JS library, say `Angular` or `jQuery`. – Rohit416 Jun 30 '16 at 06:54