Everything works fine when I wrote the js logic in a closure as a single js file, as:
(function(win){
//main logic here
win.expose1 = ....
win.expose2 = ....
})(window)
but when I try to insert a logging alternative function before that closure in the same js file,
window.Glog = function(msg){
console.log(msg)
}
// this was added before the main closure.
(function(win){
//the former closure that contains the main javascript logic;
})(window)
it complains that there is a TypeError:
Uncaught TypeError: (intermediate value)(...) is not a function
What did I do wrong?
This question is related to
javascript
typeerror
**Error Case:**
var handler = function(parameters) {
console.log(parameters);
}
(function() { //IIFE
// some code
})();
Output: TypeError: (intermediate value)(intermediate value) is not a function *How to Fix IT -> because you are missing semi colan(;) to separate expressions;
**Fixed**
var handler = function(parameters) {
console.log(parameters);
}; // <--- Add this semicolon(if you miss that semi colan ..
//error will occurs )
(function() { //IIFE
// some code
})();
why this error comes?? Reason : specific rules for automatic semicolon insertion which is given ES6 stanards
When I create a root class, whose methods I defined using the arrow functions. When inheriting and overwriting the original function I noticed the same issue.
class C {
x = () => 1;
};
class CC extends C {
x = (foo) => super.x() + foo;
};
let add = new CC;
console.log(add.x(4));
this is solved by defining the method of the parent class without arrow functions
class C {
x() {
return 1;
};
};
class CC extends C {
x = foo => super.x() + foo;
};
let add = new CC;
console.log(add.x(4));
For me it was much more simple but it took me a while to figure it out. We basically had in our .jslib
some_array.forEach(item => {
do_stuff(item);
});
Turns out Unity (emscripten?) just doesn't like that syntax. We replaced it with a good old for-loop and it stoped complaining right away. I really hate it that it doesn't show the line it is complaining about, but anyway, fool me twice shame on me.
Error Case:
var userListQuery = {
userId: {
$in: result
},
"isCameraAdded": true
}
( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;
Output:
TypeError: (intermediate value)(intermediate value) is not a function
Fix: You are missing a semi-colon (;) to separate the expressions
userListQuery = {
userId: {
$in: result
},
"isCameraAdded": true
}; // Without a semi colon, the error is produced
( cameraInfo.findtext != "" ) ? searchQuery : userListQuery;
Every line that begins with a (
, [
, `, or any operator (/, +, - are the only valid ones), must begin with a semicolon.
func()
;[0].concat(myarr).forEach(func)
;(myarr).forEach(func)
;`hello`.forEach(func)
;/hello/.exec(str)
;+0
;-0
This prevents a
func()[0].concat(myarr).forEach(func)(myarr).forEach(func)`hello`.forEach(func)/hello/.forEach(func)+0-0
monstrocity.
To mention what will happen: brackets will index, parentheses will be treated as function parameters. The backtick would transform into a tagged template, and regex or explicitly signed integers will turn into operators. Of course, you can just add a semicolon to the end of every line. It's good to keep mind though when you're quickly prototyping and are dropping your semicolons.
Also, adding semicolons to the end of every line won't help you with the following, so keep in mind statements like
return // Will automatically insert semicolon, and return undefined.
(1+2);
i // Adds a semicolon
++ // But, if you really intended i++ here, your codebase needs help.
The above case will happen to return/continue/break/++/--. Any linter will catch this with dead-code or ++/-- syntax error (++/-- will never realistically happen).
Finally, if you want file concatenation to work, make sure each file ends with a semicolon. If you're using a bundler program (recommended), it should do this automatically.
I have faced this issue when I created a new ES2015 class where the property name was equal to the method name.
e.g.:
class Test{
constructor () {
this.test = 'test'
}
test (test) {
this.test = test
}
}
let t = new Test()
t.test('new Test')
Please note this implementation was in NodeJS 6.10.
As a workaround (if you do not want to use the boring 'setTest' method name), you could use a prefix for your 'private' properties (like _test
).
Open your Developer Tools in jsfiddle.
Source: Stackoverflow.com