如何在javascript范围内传递所有表达式

如何在javascript范围内传递所有表达式,javascript,Javascript,这个问题很基本,, 下面是我想在javascrtipt中执行的示例代码: (function getdata(){ gettrue( true=true ); gettrue( 1==2 ); gettrue( 1==1 ); // write something here to get all three expressions result ? // console.log( [ all three result ] ) })() gettrue(){} 更新: 此问

这个问题很基本,, 下面是我想在javascrtipt中执行的示例代码:

(function getdata(){
  gettrue( true=true );
  gettrue( 1==2 );
  gettrue( 1==1 );
  // write something here to get all three expressions result ? 
  // console.log( [ all three result ] )
})()
gettrue(){}

更新: 此问题可用于测试框架:

test("several test result:" , function(){
   assert("test1" , true );
   assert("test2" , false )
 })

function test(title , fn){
   console.log("running: " + title)
   fn()
   // write something here to get all result of assert
   //  not more than 2 lines
  }

function assert(str, test){
   if(test){
     console.log(str +" :  PASS ")
   }else{
     console.log(str +" :  Fail ")
   }
   return test
}
 

您需要将结果存储在某个地方。事实上,你把三个都扔掉了

数组似乎最简单:

(function getdata() {
  var results = [
    gettrue( true==true ),
    gettrue( 1==2 ),
    gettrue( 1==1 )
  ];

  console.log(results);
})()

我不确定我是否理解这个问题,但你是否在寻找类似的东西?此外,您不能将true设置为任何值。您的意思是true==true吗

var values = [];

(function getdata(){
  values.push(gettrue( true==true ));
  values.push(gettrue( 1==2 ));
  values.push(gettrue( 1==1 ));

  console.log(values);
})();

function gettrue(val){ return val; }

分配
true=true
无效。
gettrue()
没有返回任何内容,并且缺少参数

function gettrue(b) { return !!b; }
(function getdata() {
    console.log([
        gettrue(true == true),
        gettrue(1 == 2),
        gettrue(1 == 1),
    ]);
})();

最后,与几位经验丰富的程序员进行了讨论。 如果没有touch getture或assert函数,就不可能得到所有的求值结果

var count;
(function getdata(){
  count=0 

  gettrue( true );
  gettrue( 1==2 );
  gettrue( 1==1 );

  gettrue( count==3 )
})()

function gettrue(test ){
  console.log( test ) 
  count++
}
对于测试框架,它可以是:

    var count;

    test("several test result:" , function(){
       assert("test1" , true );
       assert("test2" , false )
     })

    function test(title , fn){
       console.log("running: " + title)
       count = 0 ;
       fn()
       var expectTotal = fn.toString().match(/assert/)

       if(expectTotal)
        assert( title , expectTotal.length == count )
      }

    function assert(str, test){
       if(test){
         console.log(str +" :  PASS ")
       }else{
         console.log(str +" :  FAIL ")
       }
       count ++
       return test
    }

加1,但我会选择
results.push()
我自己。我知道这很迂腐,但一年后我会发现这更容易阅读。我喜欢这个答案,但gettrue是不返回任何内容,我想找到一种方法直接得到3个相等的表达式。如果
gettrue()
不返回任何内容,你想记录什么?gettrue会记录、报告,但不会返回测试结果,这里的主要问题是,我想找到一种方法来查找getdata作用域中的所有表达式。这可以用于另一个目的,在一个作用域中有2个断言:test(function(){assert(true);assert(false)})function test(fn){//如何在不使用return的情况下获得这两个断言表达式。}这是拼写错误,应该是true==true。该规则不允许使用return from gettrue(),也不允许更改三行,其中包括gettruegettrue return undefined,我想直接得到3个相等的表达式请帮我们补充一些单词。谁应该沟通?和谁一起?然后呢?当gettrue只是一个虚拟对象时,为什么不将其命名为虚拟对象呢?并且-至少-在这个上下文中,您对scope是什么意思?我需要在getdata()的末尾编写一些代码,以便与gettrue()的3个参数通信。它可以用于测试上一个gettrue的所有结果。您也可以尝试:
test(function(){assert(true);assert(false)})实际上,不允许从gettrue()返回,也不允许使用gettrue()更改3行gettrue()只是一个伪问题,目的是表明这个问题与gettrue的作用无关