Javascript: What It Does
Page 1 of 1
Javascript: What It Does
Alright, back to me Scarlet. So many people were learning JavaScript but they're confused with what they're writing. What the code does, and where to replace closing parenthesis, they just didn't know. This tutorial might be your answer to start knowing JavaScript, step by step. Stop writing codes and read this topic before continuing and (maybe) repairing your current code.
Last edited by Scarlet on Sun Jul 26, 2015 8:46 am; edited 1 time in total
Re: Javascript: What It Does
Why did we use IIFE?
So many peoples underestimate iife as anonymous function that needed when we want to execute function automatically without calling. But there's two reason why did we use iife even we didn't want to. Well, number one is the function might executes automatically without calling.
- Code:
(function() {
alert("Hello");
})();
And two, the variables were labeled 'local' so if you didn't have any idea of what variable name to use, you can store it inside iife.
- Code:
(function() {
var push = arr.push('val');
})();
What happened if we didn't wrap it inside iife?
Let's just write a simple example.
- Code:
var push = arr.push('test');
Peoples discussed about "unique" names in JavaScript. And unique names means the name can't be used for a variable name because JavaScript might replaces the name as a variable name. Assume this commands:
- Code:
var close = "just so close..";
You'd have to wrap it inside iife. Because if you want to use window.close, it might returns a ReferenceError saying that close is not a function.
Last edited by Scarlet on Thu Aug 27, 2015 3:54 pm; edited 1 time in total
Re: Javascript: What It Does
Minifying your code
Well, if you want to minify your code, I'll show you steps to minify.
1. Using Online Tools
Really, online tools was the best way to minify codes, such as JavaScript. There are many tools to minify out there, it wasn't hard to search really.
2. Minify It Yourself
This is what I didn't recommend because it spends much time and you might didn't want to do this. If you want to create your minifying tool, there are a few tutorials out there that shows you how. If you want to do it yourself, then let's proceed.
Minifying is useful because it might reduce the file size about 30-90%, so if the code you've made was super ultra big, I highly recommend you to minimize the size by minifying the code. Well, let's took an example should we?
Imagine that all of your codes was combined inside a file. And the codes listed in your directory was about.. 30 codes, maybe.
- Code:
// code title: Testing
function foo(mytest) {
for(var i=0;i<mytest.length;i++) {
console.log(mytest);
}
}
// code title: Testing, Again
try {
eval(str);
} catch(e) {
console.log(e.constructor);
}
// code title: Bla bla bla.. this is the end of all codes!!!
alert('Hello World!');
// code title: Duh code again? Sorry but I need to!!!!
if(var1 == var2) {
killMe();
}else{
doThat();
}
// and blah, let's stop.
That's worse right? Then you should.. request a new localhost server. No. Actually we can minify your code.
For minifying I use commas, shorten the code so it didn't spend much line, and.. blah. You can see the way I minify my example code by myself.
- Code:
function foo(mytest) {
console.log(mytest);
} //before minified
function foo(a) { console.log(a); } //after minified
See the way I minify the code?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum