Javascript 在不使用原型的情况下向函数添加属性
是否可以在不使用原型的情况下向函数添加属性?我知道,通过原型,您可以执行以下操作:Javascript 在不使用原型的情况下向函数添加属性,javascript,Javascript,是否可以在不使用原型的情况下向函数添加属性?我知道,通过原型,您可以执行以下操作: function Gadget(name, color) { this.name = name; this.color = color; this.whatAreYou = function(){ return 'I am a ' + this.color + ' ' + this.name; } } 没有原型对象,同样的目标能否实现 查看对您的问题的评论(您实
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
没有原型对象,同样的目标能否实现 查看对您的问题的评论(您实际上并没有使用prototype),但只是为了帮助您理解,使用prototype应该如下所示:
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
请参阅对您的问题的评论(您实际上并没有使用prototype),但为了帮助您理解,使用prototype将如下所示:
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
你问的问题有点困惑。您当前没有将原型(无法从您的问题中判断您是否意识到这一点)用于方法或属性,如果您使用
new
函数创建对象,则该技术可以正常工作,如下所示:
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
工作演示:
当使用new
操作符时,它会创建一个新对象,并调用分配给新对象的this
函数。您添加到对象中的、此指向构造函数的任何属性都将成为新对象的属性
事实上,在您的示例中,具有动态值(如name
和color
)的属性通常在构造函数中这样分配,因为为它们使用原型没有什么好处,因为它们被分配了动态值。使用原型分配方法(如whatAreYou
方法)有一个性能优势,因为在构造函数运行时需要运行的代码更少——尽管差别不大
为了进行比较和对比,使用原型定义方法的代码如下所示:
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
如果您只是简单地调用函数,如下所示:
Gadget();
然后,不会创建新对象,
此
将指向全局对象,或者将是未定义的
(在严格模式下),因此属性不会位于特定于Gadget的对象上。对于您要问的问题,有一点困惑。您当前没有将原型(无法从您的问题中判断您是否意识到这一点)用于方法或属性,如果您使用new
函数创建对象,则该技术可以正常工作,如下所示:
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
工作演示:
当使用new
操作符时,它会创建一个新对象,并调用分配给新对象的this
函数。您添加到对象中的、此指向构造函数的任何属性都将成为新对象的属性
事实上,在您的示例中,具有动态值(如name
和color
)的属性通常在构造函数中这样分配,因为为它们使用原型没有什么好处,因为它们被分配了动态值。使用原型分配方法(如whatAreYou
方法)有一个性能优势,因为在构造函数运行时需要运行的代码更少——尽管差别不大
为了进行比较和对比,使用原型定义方法的代码如下所示:
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
var x = new Gadget("Faucet", "red");
x.whatAreYou(); // returns 'I am a red Faucet'
如果您只是简单地调用函数,如下所示:
Gadget();
然后,不会创建新对象,
此
将指向全局对象,或者将是未定义的
(在严格模式下),因此属性不会位于特定于小工具的对象上。此处不使用原型。在实例化该“类”时,请确保使用新建
或此
将绑定到最外层的作用域。请注意,您不是在向函数添加属性,而是在向对象添加属性,如果您使用new Gadget()调用该函数,将创建该对象
.oo感谢您的更正。这里没有使用原型。当您实例化“类”或时,请确保使用new
。此
将绑定到最外层的范围。请注意,您没有向函数添加属性,如果使用new Gadget()
,调用函数,则将创建对象的属性添加到该对象中。感谢您的更正。