I was doing writing some various things in JavaScript and ending up writing 2 scripts that proved useful in quite a lot of my other programs and I was wondering if there was anything that I could do better because I'm fairly new to JavaScript and jQuery after a fairly long history of Java/C++.
Both programs have __
passed to them as I have used this as my global variable to prevent cluttering of the actual global namespace (if that is what you call it) and from what I can tell objects that get passed get passed as pointers but values do not, which I still find hard to fully understand in a weakly typed language like JS. The reason they are actually in the functions in the first place is to prevent it from cluttering the global namespace if one of you wants to use it and has underscore.js or simply use __
somewhere else.
The first one basically manages inline consoles that look better in my pages while debugging and creates an assert function. log
assigns classes based on the second parameter, and these classes are listed in every style sheet I make which is fairly annoying, but I have yet to find a better way to do this. I could maybe assign them CSS styles directly, but that would assign it to every object separately and waste memory.
console.js
var log,assert;
!function(__){
__.hasConsole = true;
const ilconsole = $("#console-log");
log = function(text,level){
if (!ilconsole[0]) return false;
level = "log-" + (level || "log"); //log-log for best log 2012
ilconsole.append('<span class = "'+level+'">'+text+'</span></br>');
return true;
};
assert = function(a){
if(!a){
var n = new Error("Assertion failed");
log(n.stack,"fatal"); //I love stacks, but I hate the way this looks ;c
throw (n);
//mabye could replace all this with 'throw new Error("Assertion failed");' or even 'throw "assertion failed";'
}
};
$(document).ready(function(){
if (!log("Inline console found","important")){
log = function(f){console.log(f);}; //Prevents the second paramater interfering with default consoles that take a second paramater
log("No inline console found, defaulting to browser console. Add an object with id 'console-log' into the document to enable inline console.");
__.hasConsole = false;
}
});
}(__ || !function(){throw new Error("Global namespace (__) must be set to an object!");}()); //I don't know if this is bad practice but it stops it from running
The second one is a way to add inputs that have the text disappear when you focus them and have a button linked if you press enter while focused. The difference between this and $(this)
are obvious in the object properties but I don't know how to speeds compare, so I went with the basic JavaScript for what I could as I assumed it was faster. This may cause cache-misses but I don't think those are important in JavaScript in general because this isn't being called 120 times a second like things in C++.
jbase.js
!function(__){$(document).ready(function(){
__.hasjbase = true;
$(".jtext")
.text(function(){this.value = $(this).attr("jvalue");})
.focus(function(){
const $this = $(this);
if(this.value == $this.attr("jvalue"))
$this.val("");
})
.blur(function(){
const $this = $(this);
if(this.value === "") // I dont know if this is right (what if they type 'false' or '0' or 'undefined')
$this.val($this.attr("jvalue"));
});
$(".jlink")
.keyup(function(e){
if (e.which != 13) return;//return key
const $this = $(this);
const $that = $("#" + $this.attr("jlink"));
assert($that[0]);
$this.blur(); //unneeded?
$that.focus();
$that.click();
});
});}(__ || /*!function(){throw new Error("Global namespace (__) must be set to an object!");}()*/ {});//throw is commented because the __ isnt used importantly this script
//Example usage
/*
Input that changes to blank when focused, and activates foo when enter is hit
<input type = "text" class = "wow so fresh jtext jlink" jvalue = "cool input!" jlink = "foo"></input>
<input type = "button" id = "foo"></input>
Input that changes to blank when focused
<input type = "text" class = "wow so fresh jtext" jvalue = "cool input!"></input>
*/
x === ""
will not false-positively on0, null, false, undefined, whitespace, etc
it will work as you want, edit: unless you want to be false when they type 0, etc \$\endgroup\$