Coffeelint is telling me I have implicit parens. I'm trying to find what is causing this error.
#309: Implicit parens are forbidden.
Here's my code:
((factory) ->
if typeof module == 'object' and module.exports
module.exports = factory
else
factory(Highcharts)
return
)(Highcharts) ->
...
if seriesTypes.map
seriesTypes.map::exportKey = 'name'
if seriesTypes.mapbubble
seriesTypes.mapbubble::exportKey = 'name'
if seriesTypes.treemap
seriesTypes.treemap::exportKey = 'name'
return
###The entire block over code is one function.
Anyone give this a shot?
I think there's an issue with your code. Look at JS generated:
(function(factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory;
} else {
factory(Highcharts);
}
})(Highcharts)(function() {
...
});
As the first function returns undefined there's an error trying to call undefined as a function.
Actually no_implicit_parens is for:
# This rule prohibits implicit parens on function calls.
# Some folks don't like this style of coding.
myFunction a, b, c
# And would rather it always be written like this:
myFunction(a, b, c)
With this option on, you must put brackets around any arguments list of any function call.
To make your code working you may do the following:
((factory) ->
...
)(Highcharts(->
...
))
These brackets around the callback function do the trick. But as I said, I'm sure there's an issue with your code and the fix actually makes no much sense to me :)
Related
I spotted that when I'm using while loop, cypress does not work, so instead of while loop I found this kind of solution:
const functionName = () => {
if ( a != b ) {
this.functionName();
} else {
cy.get(".selector").click()
}
};
This block of code (typescript) works really well, but 'this' is highlighted in red and the message appears: "Object is possibly 'undefined'."
Any idea how can I to get rid off this error?
Assuming it's a typescript error, add the Non-null assertion operator
// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
// Throw exception if e is null or invalid entity
}
function processEntity(e?: Entity) {
validateEntity(e);
let s = e!.name; // Assert that e is non-null and access name
}
For reference, How to suppress "error TS2533: Object is possibly 'null' or 'undefined'" answers the same question.
You probably don't need the this. prefix at all, since the function is defined on the current scope (not a class method).
New to javascript,but i know that form
<script>(function(){ somecode })();</script>
will immediately run somecode when it be interpreted.But when i was reading a html5 game source code,I encountered some code which form like this:
<script>(function(){})</script>
There is no parentheses attached.So what does it mean?
source code:https://github.com/oxyflour/STGame
and the index.html has code form like below:
<script>(function(){})</script>
Refer the game.js file, they are using it as nodes that don't do anything(#Shilly) and accessing them with id in the script tag. I don't know what is being done with the d object but certainly it is being called somewhere, look how they're using the innerHTML
else if (v.tagName == 'SCRIPT' && $attr(v, 'type') == 'text/html') {
d[v.id] = v.innerHTML;
} else if (v.tagName == 'SCRIPT') {
d[v.id] = eval(v.innerHTML)(_t);
}
An example of what's being done:
eval(document.getElementById('myscript').innerHTML)('test');
<script id="myscript">
(function(a) {
a ? console.log(a) : console.log('some string')
})
</script>
In the game.js source I found this piece of code after a quick scan:
// ... line 702
else if (v.tagName == 'SCRIPT') {
d[v.id] = eval(v.innerHTML)(_t);
}
// ...
So it's getting the innerHTML of the <script/> tag and executing it via eval().
As far as I can tell, this does nothing. It only evaluates and returns, so no call or anything.
The (function(){})() is called an IIFE - Immediately Invoked Function Expression). This, however, is an IIFE without the last parentheses (() at the end), which means it will not be called right after its definition.
Therefore, the purpose of this code might be either to keep for later use or maybe the person writing it forgot to call it or something similar. It's not a pattern that is actually used anywhere that I have seen.
I am trying to understand the jQuery source and, the beginning part is a bit confusing. I have put that section of code over here (metaphorically) with some comments. Can anyone explain what exactly is happening and why is it written like so?
(function(global, factory) {
// When & how will it actually satisfy the below if condition? When I run this in chrome, this never seems to go into this condition. Nor did I find the variable 'module' defined/declared anywhere in the code.
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = global.document ? factory(global, true) : function(w) {
// What is the 'w' parameter ? Where is it being passed ?
if (!w.document) {
throw new Error("jQuery requires a window with a document");
}
return factory(w);
};
} else {
factory(global);
}(typeof window !== "undefined" ? window : this, function(window, noGlobal) {
// jQuery Stuff!!!
}));
You should always take a look at the original not minified code that has all comments (github: jquery).
This code detects in which environment jQuery is loaded to ensure that window exits or can be provided jquery/src/wrapper.js:
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
I am a lazy developer... I like to make shortcuts, so I bound console.info to c
/*core_functions.js*/
/*==================================================
Bind C to be alert on mobile console.log in desktop
================================================== */
window.c = false;
if (typeof console === "object" && typeof console.error === "function" && !SP.isMobile) {
c = function (msg) {
console.info(msg);
};
} else {
c = function (msg) {
debug(msg);
};
}
/*somefile.js*/
c(anObject);
I have been using this for quite some time and something has always annoyed me. It displays the file in which the function is defined rather than where the function is called from:
core_functions.js:40 Object {stadiumname: "Stadium 3", pitch: Object, lights: Object, seats: Object, scoreboards: Object…}
Can I reference where the function is being called from? Or am I forever stuck with this minor annoyance?
You are seeing that line number because it really is where console.info is called. A way I can think to avoid that is by calling the real console method rather than proxying with an intermediate function.
It seems to me you want to have an abstraction layer to handle your logging using the native console or a custom one.
In that case you could try something like this:
var c = (typeof console === 'object') ? console : alternative;
And then use it as:
c.log('Hello World');
That way your alternative object could be one with the methods you are considering on using, for instance:
var alternative = {
log: function() {
window.alert.apply(window, arguments);
}
};
This way you won't get your c() function but, I think, you'll end up with a more flexible solution. By exposing the same interface that console has you could even add these logging facilities to any code running in your app (that is, 3rd party code using native console)
window.console = (typeof window.console === 'object') ? window.console : alternative
or if you are feeling very lazy that day :)
window.console = window.console ||Â alternative
This is no perfect technique at all but if you are only using it while developing it might help.
Also, I never really participate in Stack Overflow so I'm very sorry if I'm ignoring any rule or etiquette :)
I use the following to get backtraces, you might be able to do something with it.
arguments.callee.caller.toString()
I am trying to use the following code:
$('body').bind('ajaxSend', function (elm, xhr, s) {
if (s.hasContent && securityToken) { // handle all verbs with content
var tokenParam = "__RequestVerificationToken=" + encodeURIComponent(securityToken);
s.data = s.data ? [s.data, tokenParam].join("&") : tokenParam;
// ensure Content-Type header is present!
if (s.contentType !== false || options.contentType) {
xhr.setRequestHeader( "Content-Type", s.contentType);
}
}
});
I found this in the post Stack Overflow post
With typescript I get a syntax error on "options" saying "the name options does not appear in the current scope.
Can someone help and explain why I am getting this error. I see options is not declared and wonder where it comes from if it's not declared.
It's just like with JavaScript where, at run-time, any enclosing function scopes and then the global scope are searched for an options variable and if it isn't found anywhere an exception is thrown because it's not defined.
So presumably, in the example code you pulled this from, you should assume that options is defined elsewhere, and you'd need to do the same.
I believe the slight mistake is that this line:
if (s.contentType !== false || options.contentType) {
Should actually be:
if (s.contentType !== false || s.contentType) {
As your parameter s is the ajaxOptions passed to your function by jQuery.