未捕获的TypeError:test不是函数

时间:2017-01-21 21:41:56

标签: javascript

由于某种原因,当我给函数命名时,我收到错误。

但是,如果我将它设为匿名函数并将其分配给变量,那么我可以调用该函数。

function test(){
    console.log(this)
}

test();
//************** 
// recursion 1
//************** 
var recursion = function(n){
    if(n==0){
        // console.log('yup');
        return 'success';
    }
    // console.log(n)
    return recursion(n - 1);
}

var x = recursion(10);

// console.log(x);

//************** 
// recursion 2
//************** 

var countDownFrom = function(x){
    if(x === 0){
        return true;
    }
    // console.log(x)
    return countDownFrom(x-1)
}

// console.log(countDownFrom(10))
//************** 
// fibonacci
//************** 

// console.time('fib')
function fibonacci(){
    var a = 0,
    b = 1,
    result = b;


    for(var i =0; i<100; i++){
        console.log(result);
        result = a + b;
        a=b;
        b=result;
    }
}

// console.log(fibonacci())
// console.timeEnd('fib')

//************** 
// removeDuplicate
//**************

console.time('dups')
function removeDuplicate(arr){
  var exists ={},
  outArr = [], 
  elm;

  for(var i =0; i<arr.length; i++){
    elm = arr[i];
    if(!exists[elm]){
      console.log(exists);
      outArr.push(elm);
      exists[elm] = true;
      console.log(exists);
      console.log(outArr);
      console.log(elm);
  }
}
return outArr;
}


removeDuplicate([1,3,3,3,1,5,6,7,8,1]);
console.timeEnd('dups')

//************** 
// mergeSorting
//**************
function mergeSortedArray(a, b){
  var merged = [], 
  aElm = a[0],
  bElm = b[0],
  i = 1,
  j = 1;

  if(a.length ==0)
    return b;
if(b.length ==0)
    return a;
  /* 
  if aElm or bElm exists we will insert to merged array
  (will go inside while loop)
   to insert: aElm exists and bElm doesn't exists
             or both exists and aElm < bElm
    this is the critical part of the example            
    */
    while(aElm || bElm){
       if((aElm && !bElm) || aElm < bElm){
         merged.push(aElm);
         aElm = a[i++];
     }   
     else {
         merged.push(bElm);
         bElm = b[j++];
     }
 }
 return merged;
}

//************** 
// swap number without temp
//**************

function swapNumb(a, b){
  console.log('before swap: ','a: ', a, 'b: ', b);
  b = b -a;
  a = a+ b;
  b = a-b;
  console.log('after swap: ','a: ', a, 'b: ', b);  
}

swapNumb(2, 3);

//************** 
// JS reverse string
//**************

function reverseString(str) {
    var newString = "",
    stringLength = str.length;

    for (var i = stringLength - 1; i >= 0; i--) {
        newString += str[i];
    }
    return newString;
}
var newString = reverseString('hello');
console.log(newString);

var test = "yoyo";
console.log(test += 'asdfa')

//************** 
// JS Reverse Word
//**************

function reverseWords(str){
 var rev = [], 
     wordLen = 0;
 for(var i = str.length-1; i>=0; i--){
   if(str[i]==' ' || i==0){
     rev.push(str.substr(i,wordLen+1));
     wordLen = 0;
   }
   else
     wordLen++;
 }
 return rev.join(' ');
}

var str = "lets go all day";
console.log(str.substr(11,5))
var s = reverseWords(str);
console.log(s);

//************** 
// JS Palindrome
//**************   


function isPalindrome(str){
  var i, len = str.length;
  for(i =0; i<len/2; i++){
    if (str[i]!== str[len -1 -i])
       return false;
  }
  return true;
}

isPalindrome('madam');
isPalindrome('toyota');

//************** 
// JS this
//**************  

function test(){
    console.log(this)
}

test();

3 个答案:

答案 0 :(得分:1)

这可能是一个悬而未决的问题。如果您的代码段上方有更多JS代码,则可能已将private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 1 || e.ColumnIndex == 2) { // one of the button columns was clicked ButtonHandler(sender, e); } } private void ButtonHandler(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 1) { MessageBox.Show("Column 1 button clicked at row: " + e.RowIndex + " Col: " + e.ColumnIndex + " clicked"); // call method to handle column 1 button clicked // MethodToHandleCol1ButtonClicked(e.RowIndex); } else { MessageBox.Show("Column 2 button clicked at row: " + e.RowIndex + " Col: " + e.ColumnIndex + " clicked"); // call method to handle column 2 button clicked // MethodToHandleCol2ButtonClicked(e.RowIndex); } } 分配给其他内容。

这会导致TypeError,因为在您调用它时environments: local: dataSource: logSql: true formatSql: true 将是一个字符串:

MaritimeTransportationCharges joinTable: [name:"join_table_name", key: "key", column: "column"]

这样可行,因为test在您调用之前就已分配给函数:

test

在第一个例子中,这就是解释器的作用,或多或少:

  1. 将功能定义分配给// ... some code var test = 'This is a test.'; // ... more code function test() { console.log(this); } test(); 。 (这首先发生是因为它将函数声明提升到其他所有内容之上)
  2. test重新分配为字符串。
  3. 调用// ... some code var test = 'This is a test.'; // ... more code test = function () { console.log(this); } test(); ,这是一个字符串。
  4. 第二个例子:

    1. 声明test没有值(test)。
    2. test分配给字符串。
    3. test重新分配给某个功能。
    4. 调用undefined,这是一个函数。

答案 1 :(得分:0)

看起来没问题,当你在函数

中调用this时,该函数会打印一个对象

&#13;
&#13;
function test() {
    console.log(this)
}

test()
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我刚刚对你的代码做了一个 CTRL + F ,看来你已经在上面的范围内定义了一个名为test的变量。请编辑您的代码以删除上面声明的测试变量