javascript - TypeError: console.log(...) is not a function -


i'm confused how can console.log not function on line 1091. if remove closure below, line 1091 doesn't complain such error. chrome version 43.0.2357.130 (64-bit).

enter image description here

here code:

$scope.columnnamechanged = function (tablecolumn) {     setdirtycolumn(tablecolumn);     //propagate changes key fields     (var = 0; < $scope.tableindexes.length; ++i) {         (var j = 0; j < $scope.tableindexes[i].columnname.length; ++j) {             if ($scope.tableindexes[i].columnname[j] === tablecolumn.previousname) {                 console.log('xxx', $scope.tableindexes[i].columnname[j])                 (function (i, j) {                     $timeout(function () {                         console.log($scope.tableindexes[i].columnname[j])                         $scope.tableindexes[i].columnname[j] = tablecolumn.name.touppercase();                         console.log($scope.tableindexes[i].columnname[j])                     });                 })(i, j);             }         }     } }; 

solution

simply put semicolon (;) after console.log().


explanation

the error reproducible this:

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

it’s trying pass function(){} argument return value of console.log() not function undefined (check typeof console.log();). because javascript interprets console.log()(function(){}). console.log is function.

if didn’t have console object you’d see

referenceerror: console not defined

if had console object not log method you’d see

typeerror: console.log not function

what have, however, is

typeerror: console.log(...) not function

note (...) after function name. it’s referring return value of function.


respect ;

all these code snippets result in sorts of unexpected errors if no semicolons present:

console.log() // covered before () // typeerror: console.log(...) not function  console.log() // accessing property 0 of property 1 of return value… [1][0] // typeerror: console.log(...) undefined  console.log() // undefined-3 -3 // nan 

another example

you see (...) oftentimes use of chained methods or chained property accessors:

string.match(/someregex/)[0] 

if regex isn’t found, method return null , property accessor on null cause typeerror: string.match(...) null — the return value null. in case of console.log(...) return value undefined.


Comments

Popular posts from this blog

c# - Where does the .ToList() go in LINQ query result -

Listeners to visualise results of load test in JMeter -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -