问题描述
好的,想要开始做出更积极的贡献,而不仅仅是给某人一个人。这是我用'priveleged
方法'发现的东西。 a Douglas Crockford
(
如果你想让一个构造函数使用priveleged方法,那么方法必须在
中定义构造函数ABOVE在哪里调用它们。考虑以下
:
函数ExaminableField(element,examCriteria)
{
var formElement =元素;
var name = element.name;
var required = true;
//必须在使用前定义私下
this.isRequired = function()
{
if(arguments.length == 1)
{
required = arguments [0]
}
需要退货;
}
if(!! examCriteria)
{
this.isRequired(examCriteria.required);
}
}
当我的if块高于定义
of this.isRequired时,导致错误。认为这可能有助于某人,和/或贡献
到适当的约定。
OK, wanted to make a more positive contribution to start than just
plonking somebody. Here''s something I''ve discovered using "priveleged
methods" a la Douglas Crockford
(http://crockford.com/javascript/private.html):
If you want a constructor to utilize priveleged methods the methods must
be defined in the constructor ABOVE where they are called. Consider the
following:
function ExaminableField(element, examCriteria)
{
var formElement = element;
var name = element.name;
var required = true;
//must be defined before used privately
this.isRequired = function()
{
if (arguments.length == 1)
{
required = arguments[0]
}
return required;
}
if (!!examCriteria)
{
this.isRequired(examCriteria.required);
}
}
It had been causing an error when I had my if block above the definition
of this.isRequired. Thought this might help somebody, and/or contribute
to a proper convention.
推荐答案
!!将其操作数转换为布尔值。如果它是假的,它会产生假,
否则它会产生真实。但在一个if,falsy值是可以的,所以!!
浪费了。最好写一下
if(examCriteria)
!! converts its operand to a boolean. If it was falsy, it produces false,
otherwise it produces true. But in an if, falsy values are ok, so the !! is
wasted. It is better to write
if (examCriteria)
因为分配给 - this.isRequired的函数是一个函数
表达式它在构造函数执行时被内联计算,所以
将没有赋值给 - this.isRequired - 执行之前
分配。
相比之下,内部函数定义(基于类的私有方法
OO术语)在创建变量期间进行评估。 />
对象,因为脚本进入函数的执行上下文
call [1]。所以应该可以在代码中调用这样一个函数,它在物理上先于它的定义,并且通过将对它的引用分配给公共对象来呈现该函数对象
特权。成员: -
函数ExaminableField(element,examCriteria){
var formElement = element;
var name = element.name ;
var required = true;
if(examCriteria){
_requed(examCriteria.required);
}
this.isRequired = _requed;
function _requed(){
if( arguments.length == 1){
required = arguments [0]
}
需要退货;
}
}
- 这样做似乎没必要。如果我希望这个方法是
特权,我会忍受不得不按照要求的顺序编写
构造函数。
[1]在这方面,Mozilla / Gecko的实现似乎有点偏离规范(ECMA
262 3rd Edition),因为它处理内部函数
定义块,例如 - if(x){function doX(){
....}} - 好像它们在某种意义上是内联的。以这种方式使用内部函数定义是不寻常的,因此应该没有
重大后果。
Richard 。
Because the function assigned to - this.isRequired - is a function
expression it is evaluated inline as the constructor executes so there
would be no value assigned to - this.isRequired - prior to the execution
of the assignment.
In contrast, inner function definitions (private methods in class based
OO terminology) are evaluated during the creation of the "variable"
object as the script enters the execution context for the function
call[1]. So it should be possible to call such a function in code that
physically precedes its definition and also render that function object
privileged by assigning a reference to it to a public object member:-
function ExaminableField(element, examCriteria){
var formElement = element;
var name = element.name;
var required = true;
if(examCriteria){
_requed(examCriteria.required);
}
this.isRequired = _requed;
function _requed(){
if(arguments.length == 1){
required = arguments[0]
}
return required;
}
}
- Doing so seems unnecessary. I would just put up with having to write
the constructor in the required order if I wanted the method to be
privileged.
[1] The Mozilla/Gecko implementation seems to be a bit off spec (ECMA
262 3rd Edition) in this respect as it handles inner function
definitions contained within blocks, such as - if(x){ function doX(){
.... } } - as if they were inline in some sense. It would be unusual to
be using inner function definitions in that way so there should be no
significant consequences.
Richard.
我意识到这有点OT但我发现双重 - 当验证值组合时,它不是非常有用。例如,如果用户可以指定
参数a或参数b但不能同时指定两者,我们可以写一下
if(!! a!= !! b){
参数有效
}
显然!=音译成XOR。更详细的验证规则
当这种方式很容易表达,当简写逻辑接近
时无法理解。
这个技巧在C和java中也很方便。
Andy
I realise this is a bit OT but I find the double-not it very useful when
validating combinations of values. For instance, if the user can specify
either parameter a or parameter b but not both, we can write
if ( !!a != !!b) {
parameters are valid
}
obviously != transliterates into XOR. more elaborate validation rules are
easily expressed in this way when the longhand logic would be near
impossible to comprehend.
This also technique is also handy in C and java.
Andy
这篇关于仅供参考:先入为主的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!