命名参数和继承 [英] Named arguments and inheritance

查看:35
本文介绍了命名参数和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


之前没有发布过这个小组,但是我遇到了一个问题我不能工作

out ...并希望在这里获得一些帮助;-)


我有一个基本对象,当被称为

就可以正常使用命名参数''自己的。但是,当我调用子对象时,我收到错误

[associative array name]没有属性(在firefox中)。


我很简单测试:


财产=功能(arg){

/ **

*私有财产

* /

var name = arg.name; //必填,字段名称


/ **

*特权方法,可以公开调用,也可以访问私人

变量

* /

this.getName = function(){

返回名称;

} < br $>
};


/ **

*表格字段属性的类,扩展属性

* /

FormProperty = function(arg){

/ **

*继承

* /

this.super = Property;

this.super({name:arg.name});

this.prototype = new Property;

}

// property = new FormProperty({name:" myVar"})

property = new Property({name:" test" })

alert(property.getName());


无评论// property = new FormProperty({name:" myVar"})

得到错误。


提前感谢您的帮助。

解决方案
< br $>
Pacific Fox写道:


大家好,


还没有发布到这个群组之前,但有一个问题,我不能工作

out ...并希望在这里得到一些帮助;-)


我已经得到一个基础对象,当它自己调用

时,它可以正常运行。但是,当我调用子对象时,我收到错误

[associative array name]没有属性(在firefox中)。


我很简单测试:


财产=功能(arg){

/ **

*私有财产

* /

var name = arg.name; //必填,字段名称


/ **

*特权方法,可以公开调用,也可以访问私人

变量

* /

this.getName = function(){

返回名称;

} < br $>
};


/ **

*表格字段属性的类,扩展属性

* /

FormProperty = function(arg){

/ **

*继承

* /

this.super = Property;

this.super({name:arg.name});

this.prototype = new Property;

}

// property = new FormProperty({name:" myVar"})

property = new Property({name:" test" })

alert(property.getName());


无评论// property = new FormProperty({name:" myVar"})

得到错误。


提前感谢您的帮助。



你的问题是你的基类依赖于总是得到一个有效的

对象的参数,你没有提供它一,当你在FormProperty构造函数中
子类时。如果您的基础构造函数假设

始终获得有效参数,则不能使用此

技术进行子类化,因为您的参数可能不是

在创建时可用。


请尝试这样做:


Property = function(){

/ /使用arguments集合,一个类似于Array的内置元素,

包含传递给函数的参数

if(arguments [0])

var name = arguments [0] .name; //必需,

字段的名称

//否则,我们是子类,可以跳过这个


this.getName = function(){

返回名称;

}

};


FormProperty = function(arg){

//使用function.apply - 它使用第一个

参数调用函数Property作为this

//参见
http ://developer.mozilla.org/en/docs...Function:apply

Property.apply(this,[arg]);

}

FormProperty.prototype = new Property;

property = new FormProperty({name:" myVar"})

alert(property.getName ());


纠正我,如果我错了,但这并不能打败

命名的全部目的争论?

David Golightly写道:

Pacific Fox写道:


大家好,


没有之前发布到这个小组,但是有一个问题我不能工作

out ...并希望在这里得到一些帮助;-)


我有一个基础对象,当它自己被称为

时,可以使用命名参数。但是,当我调用子对象时,我收到错误

[associative array name]没有属性(在firefox中)。


我很简单测试:


财产=功能(arg){

/ **

*私有财产

* /

var name = arg.name; //必填,字段名称


/ **

*特权方法,可以公开调用,也可以访问私人

变量

* /

this.getName = function(){

返回名称;

} < br $>
};


/ **

*表格字段属性的类,扩展属性

* /

FormProperty = function(arg){

/ **

*继承

* /

this.super = Property;

this.super({name:arg.name});

this.prototype = new Property;

}

// property = new FormProperty({name:" myVar"})

property = new Property({name:" test" })

alert(property.getName());


无评论// property = new FormProperty({name:" myVar"})

得到错误。


提前感谢您的帮助。



你的问题是你的基类依赖于总是为其参数获得一个有效的

对象,而你却没有提供它一,当你在FormProperty构造函数中
子类时。如果您的基础构造函数假设

始终获得有效参数,则不能使用此

技术进行子类化,因为您的参数可能不是

在创建时可用。


请尝试这样做:


Property = function(){

/ /使用arguments集合,一个类似于Array的内置元素,

包含传递给函数的参数

if(arguments [0])

var name = arguments [0] .name; //必需,

字段的名称

//否则,我们是子类,可以跳过这个


this.getName = function(){

返回名称;

}

};


FormProperty = function(arg){

//使用function.apply - 它使用第一个

参数调用函数Property作为this

//参见
http ://developer.mozilla.org/en/docs...Function:apply

Property.apply(this,[arg]);

}

FormProperty.prototype = new Property;

property = new FormProperty({name:" myVar"})

alert(property.getName ());


好的,它似乎有效,但我不知道它是如何工作的构造函数

for Property没有任何争论,我有点不解......


property = new Property({name:" myVar"});


太平洋福克斯写道:


纠正我,如果我错了,但这并没有打败

命名的全部目的争论?


David Golightly写道:


Pacific Fox写道:


大家好,

>

之前没有发布到这个小组,但是遇到了一个我不能工作的问题

out ...并希望在这里得到一些帮助;-)

>

我有一个基础对象,当命名参数工作正常时它本身叫做
。但是,当我调用子对象时,我收到错误

[associative array name]没有属性(在firefox中)。

>

我简单的测试:

>

Property = function(arg){

/ **

*私有房产

* /

var name = arg.name; //必填,字段名称

>

/ **

*特权方法,可以公开调用,也可以访问私有

变量

* /

this.getName = function(){

返回名称;

}

};

>

/ **

*表单字段属性的类,扩展属性

* /

FormProperty = function(arg){

/ **

*继承

* /

this.super =物业;

this.super({name:arg.name});

this.prototype = new Property;

}

// property = new FormProperty({name:" myVar"})

property = new Property({name:" test"})

alert(property.getName());

>

Un注释// property = new FormProperty({name:" myVar"})

以获取错误。

>

谢谢进阶任何帮助。



你的问题是你的基类依赖于总是得到一个有效的

对象的参数,你没有提供它一,当你在FormProperty构造函数中
子类时。如果您的基础构造函数假设

始终获得有效参数,则不能使用此

技术进行子类化,因为您的参数可能不是

在创建时可用。


请尝试这样做:


Property = function(){

/ /使用arguments集合,一个类似于Array的内置元素,

包含传递给函数的参数

if(arguments [0])

var name = arguments [0] .name; //必需,

字段的名称

//否则,我们是子类,可以跳过这个


this.getName = function(){

返回名称;

}

};


FormProperty = function(arg){

//使用function.apply - 它使用第一个

参数调用函数Property作为this

//参见
http ://developer.mozilla.org/en/docs...Function:apply

Property.apply(this,[arg]);

}

FormProperty.prototype = new Property;

property = new FormProperty({name:" myVar"})

alert(property.getName ());


Hi all,

haven''t posted to this group before, but got an issue I can''t work
out... and hoping to get some help here ;-)

I''ve got a base object that works fine with named arguments when called
on it''s own. However when I call the child object I get an error
"[associative array name] has no properties (in firefox)."

I simple test:

Property = function( arg ) {
/**
* Private properties
*/
var name = arg.name; // required, the name of the field

/**
* Priviliged methods, may be invoked publicly and may access private
variables
*/
this.getName = function() {
return name;
}
};

/**
* Class for form field properties, extends property
*/
FormProperty = function( arg ) {
/**
* Inheritance
*/
this.super = Property;
this.super( { name:arg.name } );
this.prototype = new Property;
}
//property = new FormProperty( { name:"myVar" } )
property = new Property( { name:"test" } )
alert( property.getName() );

Un comment //property = new FormProperty( { name:"myVar" } )
to get the error.

Thanks in advance for any help.

解决方案


Pacific Fox wrote:

Hi all,

haven''t posted to this group before, but got an issue I can''t work
out... and hoping to get some help here ;-)

I''ve got a base object that works fine with named arguments when called
on it''s own. However when I call the child object I get an error
"[associative array name] has no properties (in firefox)."

I simple test:

Property = function( arg ) {
/**
* Private properties
*/
var name = arg.name; // required, the name of the field

/**
* Priviliged methods, may be invoked publicly and may access private
variables
*/
this.getName = function() {
return name;
}
};

/**
* Class for form field properties, extends property
*/
FormProperty = function( arg ) {
/**
* Inheritance
*/
this.super = Property;
this.super( { name:arg.name } );
this.prototype = new Property;
}
//property = new FormProperty( { name:"myVar" } )
property = new Property( { name:"test" } )
alert( property.getName() );

Un comment //property = new FormProperty( { name:"myVar" } )
to get the error.

Thanks in advance for any help.

Your problem is that your base class depends on always getting a valid
object for its argument, and you''re not providing it one when you
subclass within the FormProperty constructor. You can''t use this
technique to subclass if your base constructor makes assumptions about
always getting valid arguments, because your arguments might not be
available at creation time.

Try this instead:

Property = function() {
// use the arguments collection, an Array-like builtin that
contains the arguments passed into a function
if (arguments[0])
var name = arguments[0].name; // required, the name of the
field
// otherwise, we''re subclassing and can skip this

this.getName = function() {
return name;
}
};

FormProperty = function( arg ) {
// use function.apply - it calls function Property using the first
parameter as "this"
// See also
http://developer.mozilla.org/en/docs...Function:apply
Property.apply(this, [arg]);
}
FormProperty.prototype = new Property;
property = new FormProperty( { name:"myVar" } )
alert( property.getName() );


Correct me if I am wrong, but doesn''t that defeat the whole purpose of
named arguments?
David Golightly wrote:

Pacific Fox wrote:

Hi all,

haven''t posted to this group before, but got an issue I can''t work
out... and hoping to get some help here ;-)

I''ve got a base object that works fine with named arguments when called
on it''s own. However when I call the child object I get an error
"[associative array name] has no properties (in firefox)."

I simple test:

Property = function( arg ) {
/**
* Private properties
*/
var name = arg.name; // required, the name of the field

/**
* Priviliged methods, may be invoked publicly and may access private
variables
*/
this.getName = function() {
return name;
}
};

/**
* Class for form field properties, extends property
*/
FormProperty = function( arg ) {
/**
* Inheritance
*/
this.super = Property;
this.super( { name:arg.name } );
this.prototype = new Property;
}
//property = new FormProperty( { name:"myVar" } )
property = new Property( { name:"test" } )
alert( property.getName() );

Un comment //property = new FormProperty( { name:"myVar" } )
to get the error.

Thanks in advance for any help.


Your problem is that your base class depends on always getting a valid
object for its argument, and you''re not providing it one when you
subclass within the FormProperty constructor. You can''t use this
technique to subclass if your base constructor makes assumptions about
always getting valid arguments, because your arguments might not be
available at creation time.

Try this instead:

Property = function() {
// use the arguments collection, an Array-like builtin that
contains the arguments passed into a function
if (arguments[0])
var name = arguments[0].name; // required, the name of the
field
// otherwise, we''re subclassing and can skip this

this.getName = function() {
return name;
}
};

FormProperty = function( arg ) {
// use function.apply - it calls function Property using the first
parameter as "this"
// See also
http://developer.mozilla.org/en/docs...Function:apply
Property.apply(this, [arg]);
}
FormProperty.prototype = new Property;
property = new FormProperty( { name:"myVar" } )
alert( property.getName() );


OK, it seems to work, but I don''t see how it works as the constructor
for Property does not have any arguments, i''m a bit puzzled...

property = new Property( { name:"myVar" } );

Pacific Fox wrote:

Correct me if I am wrong, but doesn''t that defeat the whole purpose of
named arguments?
David Golightly wrote:

Pacific Fox wrote:

Hi all,
>
haven''t posted to this group before, but got an issue I can''t work
out... and hoping to get some help here ;-)
>
I''ve got a base object that works fine with named arguments when called
on it''s own. However when I call the child object I get an error
"[associative array name] has no properties (in firefox)."
>
I simple test:
>
Property = function( arg ) {
/**
* Private properties
*/
var name = arg.name; // required, the name of the field
>
/**
* Priviliged methods, may be invoked publicly and may access private
variables
*/
this.getName = function() {
return name;
}
};
>
/**
* Class for form field properties, extends property
*/
FormProperty = function( arg ) {
/**
* Inheritance
*/
this.super = Property;
this.super( { name:arg.name } );
this.prototype = new Property;
}
//property = new FormProperty( { name:"myVar" } )
property = new Property( { name:"test" } )
alert( property.getName() );
>
Un comment //property = new FormProperty( { name:"myVar" } )
to get the error.
>
Thanks in advance for any help.

Your problem is that your base class depends on always getting a valid
object for its argument, and you''re not providing it one when you
subclass within the FormProperty constructor. You can''t use this
technique to subclass if your base constructor makes assumptions about
always getting valid arguments, because your arguments might not be
available at creation time.

Try this instead:

Property = function() {
// use the arguments collection, an Array-like builtin that
contains the arguments passed into a function
if (arguments[0])
var name = arguments[0].name; // required, the name of the
field
// otherwise, we''re subclassing and can skip this

this.getName = function() {
return name;
}
};

FormProperty = function( arg ) {
// use function.apply - it calls function Property using the first
parameter as "this"
// See also
http://developer.mozilla.org/en/docs...Function:apply
Property.apply(this, [arg]);
}
FormProperty.prototype = new Property;
property = new FormProperty( { name:"myVar" } )
alert( property.getName() );


这篇关于命名参数和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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