一个函数和一个对象同时存在? [英] A function and an object at the same time?

查看:37
本文介绍了一个函数和一个对象同时存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建函数变量之后,您是否有可能像常规对象一样实际上为其分配属性?这就是我所做的:

Is it possible that after creating a function variable you can actually assign properties to it as if it's a regular object? This is what I did:

var example = function(a, b){
    console.log(a, b);
}
example.someProperty = 'hi there';

然后我在浏览器控制台中输入了以下几行:

Then I typed these lines in the browser console:

example('Hello', 'world') // Hello world
example.someProperty // hi there

因此,现在基本上,"example"变量同时充当函数和对象.这给我提出了一些问题,其中一个是原因,另一个是-有一种方法可以通过创建对象文字来做到这一点,因为我想不到这种方式.

So now basically the 'example' var acts as a function and as an object at the same time. This raised some questions for me, one of which is why, and another one - is there a way to do this by creating an object literal, because I can't think of such way.

推荐答案

所以现在基本上,"example"变量同时充当函数和对象.

So now basically the 'example' var acts as a function and as an object at the same time.

它不是充当一个函数和一个对象,它是一个函数和一个对象.函数是JavaScript中的对象.

It doesn't act as a function and an object, it is a function and an object. Functions are objects in JavaScript.

这对我提出了一些问题,其中一个是为什么

This raised some questions for me, one of which is why

从根本上讲,这是Eich在1995年5月那10天内决定要做的事情.为什么他认为那是他只能回答的问题,但是多年来,有许多语言也认为没有理由将函数视为特殊事物和不同.大概他受那些影响了.函数是正确的对象非常方便和灵活.例如:

Fundamentally because that's what Eich decided to do in those 10 days in May 1995. Why he decided that is something only he can answer, but there have been many languages through the years that also see no reason to treat functions as something special and different. Presumably he was influenced by those. It's incredibly handy and flexible that functions are proper objects. For instance:

function foo() {
    // ...
}
var f = foo;

我可以使用变量 f 来引用 foo ,因为 foo 是一个对象.在许多语言(例如Java)中,这样做确实是一件痛苦的事(尽管Java的功能有所改进,这要归功于它最近添加的lambda).

I can use the variable f to refer to foo because foo is an object. In many languages, such as Java, it's a real pain to do that (though Java is a bit better now thanks to its recently-added lambdas).

由于函数是对象,因此它们具有原型,这意味着我可以向 all 函数添加功能.例如:我发现能够接受函数并加入"非常方便(或" curry )参数:

Since functions are objects, they have a prototype, which means I can add features to all functions. For instance: I find it quite handy to be able to take a function and "bake in" (or "curry") arguments:

// A simple function
function foo(a, b) {
    console.log("a is " + a);
    console.log("b is " + b);
}

// Create a new one that, when called, will call the original passing in
// 1 as the first argument and then passing in any further arguments,
// preserving the `this` it was called with
var f = foo.curry(1);

// Call it
f(2); // "a is 1" / "b is 2"

由于JavaScript没有 curry 函数(它具有 bind ,这很相似,但是会干扰 this ),所以我可以添加一个:

Since JavaScript doesn't have a curry function (it has bind, which is similar, but interferes with this), I can add one:

var slice = Array.prototype.slice;
Object.defineProperty(Function.prototype, "curry", {
    value: function() {
        var f = this;
        var args = slice.call(arguments);
        return function() {
            return f.apply(this, args.concat(slice.call(arguments)));
        };
    }
});

瞧瞧,现在我可以在任何函数上使用 curry 了:

And voilà, now I can use curry on any function:

var slice = Array.prototype.slice;
Object.defineProperty(Function.prototype, "curry", {
  value: function() {
    var f = this;
    var args = slice.call(arguments);
    return function() {
      return f.apply(this, args.concat(slice.call(arguments)));
    };
  }
});

// A simple function
function foo(a, b) {
  snippet.log("a is " + a);
  snippet.log("b is " + b);
}

// Create a new one that, when called, will call the original passing in
// 1 as the first argument and then passing in any further arguments,
// preserving the `this` it was called with
var f = foo.curry(1);

// Call it
f(2); // "a is 1" / "b is 2"

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

是否可以通过创建对象文字来实现此目的

is there a way to do this by creating an object literal

否,创建函数的唯一方法是从函数开始.您不能将非功能对象转换为功能.

No, the only way to create a function is to start out with a function. You can't take a non-function object and turn it into a function.

这篇关于一个函数和一个对象同时存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆