简体   繁体   中英

Uncaught TypeError: test is not a function

For some reason I get an error when I give the function a name..

However if I make it an anonymous function and assign it to a variable then I can call the function.

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();

This might be a hoisting issue. If you have more JS code above your code snippet, test might have been assigned to something else.

This will cause a TypeError because test will be a string by the time you call it:

// ... some code

var test = 'This is a test.';

// ... more code

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

test();

This will work because test is assigned to a function right before you call it:

// ... some code

var test = 'This is a test.';

// ... more code

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

test();

In the first example, this is what the interpreter does, more or less:

  1. Assign a function definition to test . (This happens first because it hoists function declarations above everything else)
  2. Reassign test to be a string.
  3. Invoke test , which is a string.

Second example:

  1. Declare test with no value ( undefined ).
  2. Assign test to a string.
  3. Reassign test to a function.
  4. Invoke test , which is a function.

It looks OK, the function prints an object when you call this inside the function

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

I just did a CTRL + F on your code and it seems that you already have a variable defined with the name of test in above scope. Please edit your code to remove the test variable declared above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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