限制函数在构造方法的范围内 [英] Restricting A Function To Be In Scope of Constructor Method

查看:88
本文介绍了限制函数在构造方法的范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码显示了将函数限制为构造对象的

方法的常见方法:


函数aConstructor(arg)

{

if(typeof(arg)==" undefined")

return(null);

this.property1 = arg;

this.property2 = aConstantDefinedGlobally;

this.method1 = function(anArg){

return(this.property1 + this.property2);

};

this.method2 = function(arg1,arg2){

var aVar = arg1 + this.property1;

//很多---也许100行方法代码

//。

/ /。

//。

//。

};

return(this);

}


方法''​​method1''和代码操作对象属性被添加到

使它成为显示简短位的更好示例代码。


请注意,方法''method2''在代码行方面相当长。 />

假设为了代码可读性,尝试定义

方法external。到构造函数,但它仍然是对象的方法,而不是全局范围内的函数(哦,另一个问题:

实际上是一个函数全局对象的方法?)。如上所述:


函数方法2(arg1,arg2)

{

var aVar.arg1 + this.property1;

//以下几行代码

}


函数aConstructor(arg)

{

if(typeof(arg)==" undefined")

return(null);

this.property1 = arg;

this.property2 = aConstantDefinedGlobally;

this.method1 = function(anArg){

return(this.property1 + this.property2);

};

this.method2 = method2;

return(this);

}

当然,这是尝试过并且不起作用,''this''对象是

函数method2是全局对象。


所以问题是,如何设置代码,以便方法''method2''

仍然是构造函数的方法,不应该作为函数调用
$ b全球范围内的$ b?

解决方案

Pati ent Guy写道:

< snip>


函数方法2(arg1,arg2)

{

var aVar.arg1 + this.property1;

//以下几行代码

}


函数aConstructor(arg)

{

if(typeof(arg)==" undefined")

return(null);



在构造函数中执行此操作没有意义,因为NewExpression

无法计算为原始值(包括Null)。如果你试图从构造函数返回原始值,结果仍然是构造的

对象。


this.property1 = arg;

this.property2 = aConstantDefinedGlobally;

this.method1 = function(anArg){

return(this .property1 + this.property2);

};

this.method2 = method2;

return(this);



明确返回没有意义 - 这个 - 。


}



根据您显示的代码,最佳定义是: -


函数aConstructor(arg){

这个。 property1 = arg;

}


aConstructor.prototype.method1 = function(anArg){

return(this.property1 + this.property2);

};


aConstructor.prototype.method2 = function(arg1,arg2){

var aVar .arg1 + this.property1;

//以下几行代码

};


aConstructor.prototype.property2 = aConstantDefinedGlobally;


(具有相当大的优势,即

代表方法的函数对象只创建一次并且它们的赋值是

也只制作一次。


当然,这是尝试过但不起作用, ''this''

对象是函数method2是全局对象。



在javascript中 - this - 值始终是,并且仅由

函数调用决定。你没有显示任何实例化
对象的代码或者调用那些对象的方法,因此关于 -

this -value的断言只不过是断言。


所以问题是,如何设置代码以便方法

''method2''仍然是构造函数$ b $的方法b



您发布的代码中没有任何迹象表明您创建构造函数方法的任何实际

愿望。


并且不应该在全局范围内调用

函数?



您可以控制如何调用代码。


Richard。

< br>

" Richard Cornford" < Ri ***** @ litotes.demon.co.ukwrote在

comp.lang.javascript


Patient Guy写道:

< snip>


> function method2(arg1,arg2)
{
var aVar.arg1 + this.property1;
//以下几行代码
}

function aConstructor(arg)
{
if(typeof(arg)==" undefined")
return(null);



在构造函数中执行此操作没有意义,因为NewExpression

无法计算为原始值(包括Null)。如果你试图从构造函数返回一个原始值,结果仍然是构造的

对象。



是的,我看过这个。可能更仔细地阅读

规范会告诉我,当没有对象被实例化时,

最好什么都不返回?


>


> this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function(anArg){
return(this.property1 + this.property2);
};
this.method2 = method2;
return(this);



明确返回没有意义 - 这 - 。



好​​的。


> }



根据您显示的代码,最佳定义是: -


函数aConstructor(arg){

this.property1 = arg;

}


aConstructor.prototype.method1 = function(anArg){

return(this.property1 + this.property2);

};


aConstructor.prototype.method2 = function(arg1,arg2){

var aVar.arg1 + this.property1;

//以下几行代码

};


aConstructor.prototype.property2 = aConstantDefinedGlobally;


(具有相当大的优势,即

代表方法的函数对象只创建一次及其赋值是

也只制作一次)。




这可能是我正在寻找的并将进行测试。这个

在声称运行的不同环境(浏览器)中实现得如何?

Javascript?


>当然,这是尝试过并且不起作用,''this''
对象是函数method2是全局对象。



在javascript中 - this - 值始终是,并且仅由

函数调用决定。你没有显示任何实例化
对象的代码或者调用那些对象的方法,因此关于 -

this -value的断言只不过是断言。



你可以看到在构造函数的第一个例子中我将
定义为一种方法外部。函数(全局范围内的函数),

或外部,构造函数范围之外的函数

函数。我的意图是通过将外部函数定义为构造函数的

方法,该函数将继承对象

被构造为this,但是显然情况并非如此。


当然,使用C ++,有一种符号


class :: method


可用于类外的特权方法定义

定义。我只是试图找到Javascript替代品,如果

是一个。


>所以问题是,如何设置代码以便方法
''method2''仍然是构造函数的方法



有在您发布的代码中没有任何迹象表明您有任何实际的

想要创建构造函数的方法。



我给出的构造函数的第一个例子显示了代码在
构造函数(函数)定义。


后面的例子解释了我在构造函数定义之外定义

方法的尝试(超出它的大括号

''{}'')并且还将函数定义限制为

构造函数的方法。我只是无法找到制作

限制的正确编码。


>


>并且不应该在全局范围内调用
函数?



您可以控制如何调用代码。


Richard。

<患者盖伊写道:


Richard Cornford写道:


> Patient Guy写道:



< snip>


>> function aConstructor(arg)
{
if(typeof(arg)==" undefined")
return(null);


在构造函数中执行此操作没有意义,因为NewExpression无法计算为原始值
(包括Null)。如果您尝试从构造函数返回原始值
,结果仍将是构造的对象。



是的,我看过这个。可能更仔细地阅读

规范会告诉我,当没有对象实例化时,最好不返回任何东西?



NewExpression无法评估为原始值(包括

未定义)。


<剪断>


>鉴于您显示的代码,最佳定义为: -

函数aConstructor(arg ){
this.property1 = arg;
}
aConstructor.prototype.method1 = function(anArg){
return(this.property1 + this.property2) ;
};

aConstructor.prototype.method2 = function(arg1,arg2){
var aVar.arg1 + this.property1;
//很多行代码遵循
};

aConstructor.prototype.property2 = aConstantDefinedGlobally;

(具有代表方法的函数对象的显着优势)只创建一次,他们的
任务也只进行一次)。



这可能是我正在寻找的并将进行测试。



可能不是,但是你发布的代码应该是如何编写的。


在不同环境中实现的效果如何?b $ b(浏览器)声称运行Javascript?



大多数声称运行javascript的浏览器实际上都运行javascript。


>>当然,这是尝试过并且不起作用,
''this''对象是函数method2是全局对象。


在javascript中 - this - 值总是,且仅由
决定函数的调用方式。你没有显示任何实例化对象或调用这些对象的方法的代码,因此关于 - this - value
的断言只不过是断言。



你可以在构造函数的第一个例子中看到

函数我定义为一个方法外部函数(全局范围内的
函数),或外部函数。函数

在构造函数范围之外。



声明函数的上下文(或函数表达式

已评估)会影响其范围链。


我的意图是通过将

外部函数定义为构造函数的方法,



将外部函数定义为构造函数的方法。

毫无意义。你从未指定任何函数作为

构造函数的方法。


函数将继承对象

构造为这个,但显然并非如此。



在javascript中 - 这个 - 函数中的值总是被确定,而

只能通过函数的调用方式来确定(或者每个通话基础)。


当然,使用C ++,有...



C ++是不是javascript(甚至不喜欢javascript)。


< snip>


>>所以问题是,如何设置代码以便方法
''method2''仍然是构造函数的一种方法


你发布的代码中没有任何迹象表明你有任何创建方法的实际愿望。构造函数。



我给出的构造函数的第一个例子显示了

编码为构造函数定义了方法



构造函数的定义方法比最后的

公式更无意义。你发布的代码中没有任何迹象表明你有什么实际的想要创建构造函数的方法



其中的代码是

构造函数(函数)定义。


后面的例子解释了我尝试在外面定义方法
构造函数定义

(超出其大括号''{}'')并且还将函数

定义为构造函数的方法。



除了整个构造函数的方法之外显而易见的事情

对一个对象的方法的误解,你的

帖子中没有任何内容可以用来表示你想做什么,只是用不同的方式

没有做任何事情。


我只是无法找到正确的编码来制作
$ b $那个限制。



固定你想要制作限制的原因可能是找出你需要知道什么的最快方法。


>>并且不应该在全局范围内调用
函数?


您可以控制如何调用代码。



< snip>


Richard。



The code below shows the familiar way of restricting a function to be a
method of a constructed object:

function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);
this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = function (arg1, arg2) {
var aVar = arg1 + this.property1;
// a lot---maybe 100 lines of method code
// .
// .
// .
// .
};
return (this);
}

Method ''method1'' and the code manipulating object properties was added to
make it a better example in showing brief bits of code.

Note that method ''method2'' is rather long in terms of lines of code.

Suppose for the sake of code readability, the attempt is made to define
the method "external" to the constructor, but that it still be a method of
the object, and not a function in global scope (oh, and another question:
is a function actually a method of the global object?). As so:

function method2(arg1, arg2)
{
var aVar.arg1 + this.property1;
// lots of lines of code follows
}

function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);
this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = method2;
return (this);
}

Of course, this was tried and does not work, with the ''this'' object is
function method2 is the global object.

So the question is, how can I set up the code so that method ''method2''
remains a method of the constructor and should not be called as a function
in global scope?

解决方案

Patient Guy wrote:
<snip>

function method2(arg1, arg2)
{
var aVar.arg1 + this.property1;
// lots of lines of code follows
}

function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);

There is no point in doing this in a constructor as a NewExpression
cannot evaluate as a primitive value (including Null). If you try to
return a primitive value from a constructor the result will still be the
object constructed.

this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = method2;
return (this);

There is no point in explicitly returning - this -.

}

Given the code you show the optimum definition would be:-

function aConstructor(arg){
this.property1 = arg;
}

aConstructor.prototype.method1 = function (anArg) {
return (this.property1 + this.property2);
};

aConstructor.prototype.method2 = function(arg1, arg2){
var aVar.arg1 + this.property1;
// lots of lines of code follows
};

aConstructor.prototype.property2 = aConstantDefinedGlobally;

(with the considerable advantage that the function objects that
represent the methods are only created once and their assignments is
also only made once).

Of course, this was tried and does not work, with the ''this''
object is function method2 is the global object.

In javascript the - this - value is always, and only, determined by how
a function is called. You have not shown any code that instantiates
objects or calls methods of those objects so your assertions about the -
this -value - are no more than assertions.

So the question is, how can I set up the code so that method
''method2'' remains a method of the constructor

There is no indication in your posted code than you have any actual
desire to create methods of the constructor.

and should not be called as a
function in global scope?

You are in control of how you call your code.

Richard.


"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in
comp.lang.javascript:

Patient Guy wrote:
<snip>

> function method2(arg1, arg2)
{
var aVar.arg1 + this.property1;
// lots of lines of code follows
}

function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);


There is no point in doing this in a constructor as a NewExpression
cannot evaluate as a primitive value (including Null). If you try to
return a primitive value from a constructor the result will still be the
object constructed.

Yes, I have seen this. Probably a more careful reading of the
specification will tell me that when no object is to be instantiated, it
is better to return nothing?

>

> this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = method2;
return (this);


There is no point in explicitly returning - this -.


Okay.

> }


Given the code you show the optimum definition would be:-

function aConstructor(arg){
this.property1 = arg;
}

aConstructor.prototype.method1 = function (anArg) {
return (this.property1 + this.property2);
};

aConstructor.prototype.method2 = function(arg1, arg2){
var aVar.arg1 + this.property1;
// lots of lines of code follows
};

aConstructor.prototype.property2 = aConstantDefinedGlobally;

(with the considerable advantage that the function objects that
represent the methods are only created once and their assignments is
also only made once).



This is probably what I am looking for and will test. How well is this
implemented in different environments (browsers) claiming to run
Javascript?

>Of course, this was tried and does not work, with the ''this''
object is function method2 is the global object.


In javascript the - this - value is always, and only, determined by how
a function is called. You have not shown any code that instantiates
objects or calls methods of those objects so your assertions about the -
this -value - are no more than assertions.


You can see that within the first example of the constructor function I
defined as a method an "external" function (a function in global scope),
or by "external," a function outside the scope of the constructor
function. It was my intention that by defining the external function as a
method of the constructor, that the function would inherit the object
being constructed as ''this,'' but this was clearly not the case.

With C++ of course, there is the notation

class::method

which can be used for privileged method definition outside the class
definition. I was just trying to find the Javascript alternative if there
was one.

>So the question is, how can I set up the code so that method
''method2'' remains a method of the constructor


There is no indication in your posted code than you have any actual
desire to create methods of the constructor.

The first example of the constructor function I gave showed coding that
defined methods to the constructor in which the code was within the
constructor (function) definition.

The example that followed was an explanation of my attempt to define
methods outside the constructor function definition (beyond its braces
''{}'') and also restrict the function definition as being a method of the
constructor. I just was unable to find the correct coding for making that
restriction.

>

>and should not be called as a
function in global scope?


You are in control of how you call your code.

Richard.




Patient Guy wrote:

Richard Cornford wrote:

>Patient Guy wrote:

<snip>

>> function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);


There is no point in doing this in a constructor as a
NewExpression cannot evaluate as a primitive value
(including Null). If you try to return a primitive value
from a constructor the result will still be the object
constructed.


Yes, I have seen this. Probably a more careful reading of
the specification will tell me that when no object is to be
instantiated, it is better to return nothing?

NewExpression cannot evaluate as a primitive value (including
Undefined).

<snip>

>Given the code you show the optimum definition would be:-

function aConstructor(arg){
this.property1 = arg;
}

aConstructor.prototype.method1 = function (anArg) {
return (this.property1 + this.property2);
};

aConstructor.prototype.method2 = function(arg1, arg2){
var aVar.arg1 + this.property1;
// lots of lines of code follows
};

aConstructor.prototype.property2 = aConstantDefinedGlobally;

(with the considerable advantage that the function objects
that represent the methods are only created once and their
assignments is also only made once).


This is probably what I am looking for and will test.

It probably is not, but it is how the code you posted should have been
written.

How well is this implemented in different environments
(browsers) claiming to run Javascript?

Most browsers that claim to run javascript actually do run javascript.

>>Of course, this was tried and does not work, with the
''this'' object is function method2 is the global object.


In javascript the - this - value is always, and only,
determined by how a function is called. You have not
shown any code that instantiates objects or calls methods
of those objects so your assertions about the - this - value
are no more than assertions.


You can see that within the first example of the constructor
function I defined as a method an "external" function (a
function in global scope), or by "external," a function
outside the scope of the constructor function.

The context in which a function is declared (or function expression
evaluated) influences its scope chain.

It was my intention that by defining the
external function as a method of the constructor,

"Defining the external function as a method of the constructor" is
meaningless. You never assigned any functions as methods of the
constructor.

that the function would inherit the object being
constructed as ''this,'' but this was clearly not the case.

In javascript the - this - value in a function is determined always, and
only, by how a function is called (or a per-call basis).

With C++ of course, there is ...

C++ is not javascript (not even very like javascript).

<snip>

>>So the question is, how can I set up the code so that method
''method2'' remains a method of the constructor


There is no indication in your posted code than you have any
actual desire to create methods of the constructor.


The first example of the constructor function I gave showed
coding that defined methods to the constructor

"Defined methods to the constructor" is more meaningless than the last
formulation. There is no indication in your posted code than you have
any actual desire to create methods of the constructor.

in which the code was within
the constructor (function) definition.

The example that followed was an explanation of my attempt to
define methods outside the constructor function definition
(beyond its braces ''{}'') and also restrict the function
definition as being a method of the constructor.

Apart from the whole "method of the constructor" thing being an apparent
misconception of what a method of an object is, there is nothing in your
post to suggest what it is you are trying to do, just different ways of
failing to do whatever it is.

I just was unable to find the correct coding for making
that restriction.

Pinning down why you want to make "that restriction" is probably the
quickest approach to finding out what you need to know.

>>and should not be called as a
function in global scope?


You are in control of how you call your code.

<snip>

Richard.


这篇关于限制函数在构造方法的范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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