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?
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.
Both objectOne and objectTwo are non-function objects therefore they don't have a prototype property.
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.
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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With