Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-如何将匿名方法作为变量调用并访问其属性和方法?_Javascript_Function_Variables_Anonymous Function_Language Concepts - Fatal编程技术网

Javascript-如何将匿名方法作为变量调用并访问其属性和方法?

Javascript-如何将匿名方法作为变量调用并访问其属性和方法?,javascript,function,variables,anonymous-function,language-concepts,Javascript,Function,Variables,Anonymous Function,Language Concepts,我读过,我们可以将匿名函数作为变量调用。但是,我正在尝试这样做,除此之外,我还想访问它的属性和方法。这是我的密码 var cooking = function(){ this.dessert = "Ice Cream"; this.numberOfPortions = 20; this.doubleLunch = function(){this.numberOfPortions = 40; document.write(th

我读过,我们可以将匿名函数作为变量调用。但是,我正在尝试这样做,除此之外,我还想访问它的属性和方法。这是我的密码

var cooking = function(){
        this.dessert = "Ice Cream";
        this.numberOfPortions = 20;
        this.doubleLunch = function(){this.numberOfPortions = 40;
            document.write(this.numberOfPortions);};
        };

document.write(cooking.dessert);

但我什么也没得到。你能告诉我我做错了什么吗?谢谢

烹饪
是一项功能当您调用它时,它定义了该的许多属性

该结构意味着它将用作构造函数,因此您可以使用
new
关键字创建它的实例

然后您可以与实例交互

var meal = new cooking();
document.write(meal.dessert);

注意:按照惯例,构造函数(且仅构造函数)的名称应以大写字母开头,因此您应将其重命名为Cooking。

当函数作为构造函数调用时,此
引用自身,您可以使用立即调用的函数表达式(IIFE)进行调用

演示:

但是,您可以通过使用普通的旧JavaScript对象(POJO)实现相同的结果

演示:

如果您计划多次使用构造函数,那么@Quentin提到的方法就是最好的选择

function Cooking() {
    this.dessert = "Ice Cream";
    this.numberOfPortions = 20;
    this.doubleLunch = function () {
        this.numberOfPortions = 40;
        document.write(this.numberOfPortions);
    };
}

var cooking = new Cooking();

document.write(cooking.dessert);

演示:

将其指定给变量也绝对没有意义。只是
函数Cooking(){…}
更有意义。它还将其分配给变量。一般来说,我建议使用函数声明而不是匿名函数表达式。@Quentin谢谢您的回答,但为什么需要创建它的实例呢。例如,当我执行时,它会显示数字7。是因为这个函数和我的第一个不同吗。对这是不同的。答案的第一段描述了最大的区别。我知道主要原因是我声明匿名函数的方式,不是吗?但是仍然是一个函数对象?谢谢你的回答。我和另一个答案中的约会是一样的。为什么需要创建它的实例。例如,当我执行时,它会显示数字7。是因为此函数与我的第一个函数不同吗?是因为
仅当它是实例时才引用自身,这就是为什么需要使用
new
。否则,
为外部范围,在本例中为
窗口
对象。我之所以使用“this”,是因为我想声明函数对象的属性。我错了吗?还有别的办法吗?非常感谢。只有一个问题:仍然是一个函数对象吗?@GniruT不完全确定你的意思。所有函数都是对象
var foo=function(){}
是一个函数表达式<代码>函数foo(){}是一个函数声明<代码>函数Foo(){};new Foo()是一个函数调用,返回的对象是实例。
var cooking = (function () {
    var obj = {};

    obj.dessert = "Ice Cream";
    obj.numberOfPortions = 20;
    obj.doubleLunch = function () {
        obj.numberOfPortions = 40;
        document.write(obj.numberOfPortions);
    };

    return obj;
})();

document.write(cooking.dessert);
function Cooking() {
    this.dessert = "Ice Cream";
    this.numberOfPortions = 20;
    this.doubleLunch = function () {
        this.numberOfPortions = 40;
        document.write(this.numberOfPortions);
    };
}

var cooking = new Cooking();

document.write(cooking.dessert);