Custom JavaScript log function - javascript

I am having some issues in my production code and I want to be able to send a flag to enable/disable JS logging, so I want to write my own logging function.
I want to use something like function log(){...}. When I looked up reserved words in JS I didn't see
log listed, but I do see it listed in the w3schools docs as a math function.
Is it okay to use log() as a function name in production code for IE 7+, Chrome, and FF?

Yes, it is absolutely fine. The maths log function is a function on the Math object, so will not collide with your implementation.
If you are confused by this sort of thing, look into JavaScript 'namespaces'
How do I declare a namespace in JavaScript?

Yes, it is ok. The math log function is available as Math.log, not log so you won't break anything...

That should be fine. The mathematical log function is called via Math.log(x).

Yes it is ok. The math log function always needs to be called as Math.log(). So you're fine.

I would suggest using an existing object or namespace rather than making your log function global.
See http://www.yuiblog.com/blog/2006/06/01/global-domination/

Related

GTM custom javascript is not working, what to do?

I've created custom js function. It worked on console, however I tried to transfer it to GTM using custom js variable function, it returned undefined. The function transfers a string to integer and removes whitespaces.
function(){
a= parseInt(google_tag_manager["GTM-000000000"].dataLayer.get("gtm.element").querySelectorAll("div>div>div>div")[27].querySelector("div>input").getAttribute("value").replace(/\s/g, ""));
return a}
it returns undefined((
No, the function does a lot more than you indicated. All the dom scraping The reason for it "not firing" in GTM may be due to timing. However, you're not supposed to use this kind of global scope code in GTM.
GTM kindly provides you with a {{Clicked Element}} which you can definitely use in your GTM custom JS code. So your code would become something like:
return {{Clicked Element}}.querySelectorAll("div>div>div>div")[27].querySelector("div>input").getAttribute("value").replace(/\s/g, ""));
Still, you can debug it. You can console.log your google_tag_manager["GTM-000000000"].dataLayer.get("gtm.element") from GTM and then debug it further in your console. The console nowadays allows you to copy a consol logged object, put it in a var and query against it.

Expose Function to Browser's JS Console

How can ordinary, top level functions, that are part of the default closure be exposed to the JS console in the browser (for debugging reasons)?
// default.js
function iWantToCallThisFunction() {
console.log("test successfull");
}
There's this answer from 2014, which doesn't seem to be valid anymore: https://stackoverflow.com/a/9054881/6204346
At least a current Chrome can't find the function and defaults to undefined.
Flagging the function with export doesn't seem to make a difference. It would probably be possible but inconvenient to safe the function in the window. Is there a better way?
That's very strange that it's showing as undefined. If you want to view the contents of the function (and it's already loaded and declared) just type iWantToCallThisFunction without the parenthesis at the end. If you want to call the function, just type iWantToCallThisFunction() with parenthesis. You could also try setting the function as a variable. I think that this is because the function isn't actually being created by the time you go into the JavaScript console. Make sure the right JavaScript file is connected, and that there's no errors. If the file has other stuff in it, that might be interfering with this function being made, so maybe try it in a blank file by itself.

Is there a Google app scripts function reference?

I was trying to create a function that takes a number of minutes and outputs it in a nice readable format. For instance, FORMATMINUTES(1570) would output 1d2h10m, but I got hung up trying to find a truncate function. I also had to change my lets to vars. Is there a reference that tells you what javascript features are available when writing custom functions?
To simplify I created two functions. First, the Math.trunc() function doesn't seem to exist as it doesn't show up as a suggestion and throws an error when used:
function MYTRUNC(input) {
return Math.trunc(input);
}
Second I thought that maybe I could use sheets's built-in TRUNC function, but that isn't defined either:
function MYTRUNC2(input) {
return TRUNC(input)
}
I read google's guide but it says "Custom functions are created using standard JavaScript" but not what version of javascript. Apparently it doesn't support let.
Their app scripts reference has a lot of information on interacting with sheets, but not a basic or complete reference. I realize that in this situation I can use Math.floor which is available or subtract input % 1, but I'd like to know what other idiosyncrasies there might be, and if I can use newer javascript features. Template literals give an error as well, so maybe it's based on an earlier javascript version?
The best reference I have found is from within the online Script Editor's debugger.
Make a breakpoint anywhere in any function. Then run that function with the bug button to start debugging. Next, click on the two buttons at the end of the tool bar: "Show inheritance" and "Show all data".
In the debugger, you should see a this with a "+" to expand it. Expand it and then expand [[prototype]] to see all the builtin App Script objects (GmailApp, etc), then find the next [[prototype]] below those to see the standard JavaScript objects such as Array, Math, Date, etc and you can inspect all of the functions that are available from there.

JavaScript and NPAPI use the same method name but get different identifier on Android browser

I write a plugin for android browser and follow the npruntime rule to let it support JavaScript method. However, after I call the function of my plugin in JavaScript, I get different identifier number in NPAPI's pluginHasMethod() function. I am sure there is no typo error in my JavaScript code. Is there any idea to debug this situation?
Thanks in advance.
NPN_UTF8fromIdentifier is a function you have to provide yourself to call the correct function on the NPNFuncs that you were given when the plugin initialized.
The NPIdentifier is only guaranteed to be the same during the same browser instance; if you quit the browser and start a new one, it may change.
All NPN_* functions are not real functions; those are the "typical" names that you might use for them, but in actuality you are given a structure of function pointers of type NPNFuncs that will have a function pointer for each of the NPN_* functions; if you want those to actually work you need to create the NPN_UTF8FromIdentifier function, etc, and have it call the correct function pointer.
For more info see http://npapi.com/tutorial and http://npapi.com/tutorial2

Difference between console.log enabled verification

I have seen jquery-ui-1.8.11.js verify console.log functionality by doing
if (this.debug)
console.log()
I have also seen people define an anonymous function that is a no-op for browsers with no console logging like IE7.
if(typeof console === "undefined") {
console = { log: function() { } };
}
Is there a technical difference or are both functionally equivalent?
In the first example you've given, this.debug will be a reference to a debug variable in the jQueryUI code. This debug variable will have been set elsewhere, possibly by checking whether console is defined, but also possibly with other settings.
In any case, the first example is specific to the application; if you want to do a generic check to see whether the console is available, you'll need to use the second technique. You don't have to define an empty console object as per the example, but doing it that way does mean that you won't have to do the if() condition every time you want to call console.log().
Having said all of that, I would counsel you strongly to avoid putting any code into production which contains calls to the console. The console should only be used for debugging purposes while you are working on the code. It should not be necessary in final release, and doing so can be a sign that either your code is unstable or you're unconfident of it, neither of which is a good sign if you're releasing the code for live use.
(libraries such as jQueryUI are an exception to this rule, as they need to provide functionality for developers to do debugging while writing code using their library)
Both of those do something else. The first code suppresses logging messages unless a flag is set. The people who develop jQuery UI need logging when working on it, and turn it on. But people using the library don't want to have their console cluttered by log messages from libraries, so it's off by default. It lets you turn off logging even when the browser supports it – that regular users on IE7 don't get script errors is a (possibly intended) side effect.
The second code defines a dummy console.log(), so you can call the method without checking if it exists everywhere. It will not suppress logging on browsers where it's supported.
The second of the two is standalone, not relying on jQuery. In my opinion, that makes it better.

Categories

Resources