简体   繁体   English

Javascript:没有原型的函数

[英]Javascript : functions without prototype

Built-in functions in javascript (eg Object.keys ) don't have a "prototype" property. javascript中的内置函数 (例如Object.keys )没有“ prototype”属性。

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). 但是, function(){...}new Function()都会生成一个构造函数(Function的实例)以及一个原型(Object的实例)。 Isn't it too expensive? 太贵了吗?

Is is possible to create a pure function instead of a constructor in javascript? 是否可以在JavaScript中创建纯函数而不是构造 函数

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; 在ES6中,lambda不应与其他功能关联;

(() => { }).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. 除了Math.random之外,JS中的内置函数是纯设计的。 You shouldn't derive a function with new statement which it won't do the right job for you. 您不应该使用new语句派生一个函数,它不会为您完成正确的工作。

JavaScript is a multi-paradigm programming language which exposes both functional and oop senses. JavaScript是一种多范式编程语言,它公开了functionaloop两种感觉。 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. 上面, Math可以被视为名称空间,而不是对象类型。 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: 相反,如果在JavaScript中引用原型对象的功能 ,则除非通过原型引用它们,否则将得到未定义的内容:

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. 这是由于以下事实,即Array并非像Math那样被设计为名称空间,而是OOP意义上的一类对象。 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. 否则,您可以通过prototype引用该函数以访问未绑定this对象的纯函数。 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. 其背后的含义是,JavaScript被设计为可以以OOP和Functional方式编写。 You may need to play around with function prototype as I have given you some examples above and this will clarify you further :) 您可能需要使用函数原型,因为上面已经为您提供了一些示例,这将进一步阐明您的意图:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM