简体   繁体   English

此Javascript函数返回什么?

[英]What does this Javascript function return?

function is_op(op) {
    var tok = input.peek();
    return tok && tok.type == "op" && (!op || tok.value == op) && tok;
}

The function can be found on this website: http://lisperator.net/pltut/parser/the-parser 可以在以下网站上找到该功能: http : //lisperator.net/pltut/parser/the-parser

input.peek() does return some type of a token object. input.peek()确实返回某种类型的令牌对象。 But what does return finally return? 但什么是return终于回归? True / false or the tok object itself? True / falsetok对象本身? Why does tok appear twice inside the return expression, once at the beginning and second at the end of the return expression? 为什么tok在返回表达式内出现两次,一次出现在返回表达式的开头,另一次出现在返回表达式的末尾?

It returns the object, or false. 它返回对象,或者为false。

tok appears twice inside the expression because some smarty-pants developer who hates other people being able to read his code needed to be able to return tok at the end and also evaluate it's .value property. tok在表达式中出现了两次,因为某些讨厌其他人能够读取其代码的聪明人开发人员需要能够在最后返回tok并评估其.value属性。

Here's a sensible rewrite so that everybody other than the original developer can read it: 这是一个明智的重写,以便除原始开发人员以外的所有人都可以阅读它:

//if tok is false
if(!tok) {
  return false;
}

if(tok.type !== 'op') {
  return false;
}

//if op is defined and tok.value is not the same as op
if(op && tok.value != op) {
  return false;
}

//tok.type == 'op' and tok.value == op, if it was defined
return tok;

ALSO

The dev could've written this line: 开发人员可以编写以下代码:

var tok = input.peek() || {};

which would've allowed him to not have to check if tok was false-y at the beginning: 这将使他不必一开始就检查tok为假y:

//if toke.type is 'op' and tok has a non-false-y value that is equal to op, return it
return tok.type == 'op' && (!op || tok.value == op) && tok.value == op && tok;

but a purist would also say that line ( input.peek() || {} ) allocates an object unnecessarily as well.... 但是一个纯粹主义者也会说那行( input.peek() || {} )也不必要地分配了一个对象。

The intention of the function is to return the token itself if the token (AST node) is of type operation (shortened to "op") and it matches the passed in operation type (the op parameter). 功能的目的是返回令牌本身如果令牌(AST节点)是操作类型(简称为“操作”)和它匹配于操作类型(传入op参数)。

If you don't pass in an op parameter, that function is just going to check if the token is an "op" of any type 如果你没有在传op参数,该功能只是要检查,如果令牌是任何类型的“OP”

If the conditions are not met, false is returned. 如果条件不满足,则返回错误。

The reason tok is added at the end of the boolean expression is so that tok is returned instead of true, which was the result of that last evaluation when all conditions were met. 究其原因tok在布尔表达式的末尾增加是使tok返回,而不是真实的,这是当所有条件得到满足,去年的评估结果。

Here is a more readable version 这是一个更具可读性的版本

function is_op(op) {
    var tok = input.peek();

    if (!tok || tok.type !== "op") {
       return false;
    }
    if (op && tok.value !== "op") {
       return false
    }
    return tok;
}

Since it's bad practice to use globals in your functions ( input ) and a function name is_xxx should return a boolean, I would rework the function to return a boolean and you pass in the token. 因为它是不好的做法,用在你的函数(全局input )和函数名is_xxx应该返回一个布尔值,我会返工函数返回一个布尔值,你的令牌传递。 Then the expression looks a little more digestible. 然后表达显得有些更易消化。

function is_op(tok, op) {
   return tok && tok.type == "op" && (!op || tok.value == op);
}

Then the caller would already have a reference to the token 然后调用者就已经拥有令牌的引用

var tok = input.peek();
if ( is_op(tok, "plus") ) {
   parsePlusExpression(tok);
} else {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM