Javascript - 如何将匿名方法作为变量调用并访问其属性和方法?
Javascript - How do I invoke an anonymous method as a variable and access its properties and methods?
我了解到我们可以将匿名函数作为变量来调用。但是我正在尝试这样做,除此之外我还想访问它的属性和方法。这是我的代码
var cooking = function(){
this.dessert = "Ice Cream";
this.numberOfPortions = 20;
this.doubleLunch = function(){this.numberOfPortions = 40;
document.write(this.numberOfPortions);};
};
document.write(cooking.dessert);
但我什么也没得到。你能说我做错了什么吗?谢谢
cooking
是一个函数。 当您调用它 时,它定义了 this
的许多属性。
该结构意味着它旨在用作构造函数,因此您可以使用 new
关键字创建它的实例。
然后就可以和实例进行交互了。
var meal = new cooking();
document.write(meal.dessert);
注意:约定规定构造函数(且仅构造函数)应以首字母大写开头,因此您应将其重命名为 Cooking。
this
当函数作为构造函数被调用时引用自身,您可以使用立即调用的函数表达式 (IIFE) 来实现。
var cooking = (function () {
return new function () {
this.dessert = "Ice Cream";
this.numberOfPortions = 20;
this.doubleLunch = function () {
this.numberOfPortions = 40;
document.write(this.numberOfPortions);
};
}
})();
document.write(cooking.dessert);
演示:http://jsfiddle.net/fk4uydLc/1/
但是,您可以使用普通的旧 JavaScript 对象 (POJO) 获得相同的结果。
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);
演示:http://jsfiddle.net/vmthv1dm/1/
如果您计划多次使用构造函数,那么@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);
我了解到我们可以将匿名函数作为变量来调用。但是我正在尝试这样做,除此之外我还想访问它的属性和方法。这是我的代码
var cooking = function(){
this.dessert = "Ice Cream";
this.numberOfPortions = 20;
this.doubleLunch = function(){this.numberOfPortions = 40;
document.write(this.numberOfPortions);};
};
document.write(cooking.dessert);
但我什么也没得到。你能说我做错了什么吗?谢谢
cooking
是一个函数。 当您调用它 时,它定义了 this
的许多属性。
该结构意味着它旨在用作构造函数,因此您可以使用 new
关键字创建它的实例。
然后就可以和实例进行交互了。
var meal = new cooking();
document.write(meal.dessert);
注意:约定规定构造函数(且仅构造函数)应以首字母大写开头,因此您应将其重命名为 Cooking。
this
当函数作为构造函数被调用时引用自身,您可以使用立即调用的函数表达式 (IIFE) 来实现。
var cooking = (function () {
return new function () {
this.dessert = "Ice Cream";
this.numberOfPortions = 20;
this.doubleLunch = function () {
this.numberOfPortions = 40;
document.write(this.numberOfPortions);
};
}
})();
document.write(cooking.dessert);
演示:http://jsfiddle.net/fk4uydLc/1/
但是,您可以使用普通的旧 JavaScript 对象 (POJO) 获得相同的结果。
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);
演示:http://jsfiddle.net/vmthv1dm/1/
如果您计划多次使用构造函数,那么@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);