Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript : functions without prototype

Built-in functions in javascript (e.g. Object.keys) don't have a "prototype" property.

Object.keys.prototype === undefined;             // returns true
Array.prototype.slice.prototype === undefined;   // returns true

However, either function(){...} or new Function() will generate a constructor (instance of Function) along with a prototype (instance of Object). Isn't it too expensive?

Is is possible to create a pure function instead of a constructor in javascript?

like image 443
Jingyu Lei Avatar asked May 31 '15 03:05

Jingyu Lei


People also ask

Do functions have prototype JavaScript?

A Function object's prototype property is used when the function is used as a constructor with the new operator. It will become the new object's prototype. Note: Not all Function objects have the prototype property — see description.

Which object in JavaScript does not have a prototype?

Both objectOne and objectTwo are non-function objects therefore they don't have a prototype property.

Can we create an object without prototype in JavaScript?

No. Searching [[Prototype]] in the ES3 spec shows that the only way of changing [[Prototype]] to an arbitrary given value is through [[Construct]]. However, it only works if that value is an object. If it's not (including null ), the [[Prototype]] will be set to the initial value of Object.

Do all objects have prototype in JavaScript?

Each and every JavaScript function will have a prototype property which is of the object type. You can define your own properties under prototype . When you will use the function as a constructor function, all the instances of it will inherit properties from the prototype object.


2 Answers

The question is "what does 'creating' them mean"?

For all intent and purpose,

function myFunc ( ) { }

myFunc.constructor = undefined;
myFunc.prototype = undefined;

will get you what you want, from a practical standpoint.

In ES6, lambdas should have no ties to other functions;

(() => { }).prototype === undefined; // I believe this should be 100% true

...your other question... ...is it too expensive that there are added functions/objects created for each function...

....well, there are games running happily on browsers. Talking about the memory consumption from making functions is typically immeasurably small, these days (though IoT / wearables would be a concern).
That's a premature micro-optimization.

like image 165
Norguard Avatar answered Sep 23 '22 15:09

Norguard


Apart from Math.random, built-in functions in JS are pure by design. You shouldn't derive a function with new statement which it won't do the right job for you.

JavaScript is a multi-paradigm programming language which exposes both functional and oop senses. So, you have pure functions without prototype:

Math.round // whose typeof is function
Math.floor // whose typeof is also function

Above, Math could be perceived as a namespace instead of an Object type. So it is just a container which supplies a set of functions for you.

In contrast, if you refer to functions of prototype objects in JavaScript, you will get an undefined unless you refer to them via prototype:

Array.map // undefined
Array.reduce // undefined

This is because of the matter fact that Array is not designed to be a namespace like Math, it is a class of object in OOP sense. So you need to call the function via its instance like:

var list = new Array(3);
list.map(function(a){ return 1 });

Otherwise, you may refer to the function via prototype to gain access to the pure function where this object is not bound. See following statement:

var list = [1,2,3];
typeof(Array.prototype.map); // It's pure, unbound
Array.prototype.map.call( list, function(a){ return a*2 }); // Usage in a purely-functional way

The meaning behind this is, JavaScript is designed to be writable in both OOP and Functional ways. You may need to play around with function prototype as I have given you some examples above and this will clarify you further :)

like image 25
TaoPR Avatar answered Sep 22 '22 15:09

TaoPR