let declarations Allow you to define a variable that only exist inside a block delineated by { and } Example: var a = 0; { let a = 1; console.log(a); // prints 1 } console.log(a); // prints 0 let declarations are not initialized until they are used, that is why it is a good idea […]
Category Archives: JavaScript
Ionic: the basics
$ ionic serve // Internal server to run and test your pages $ ionic build ios $ ionic emulate ios // with the other command above, emulates running the page in the iphone To use it along with your Adobe Phonegap build account (so you don’t have to build locally) Follow the instructions here to […]
JavaScript: code organization template
If not working with a framework, try the following: $(function() { function MyFunction() { } MyFunction.prototype = { myMethod: function() { } }; var myInstance = new MyFunction(); }); If you are looking for something simpler, kind of a one quick page deal: var GLOBAL_NAMESPACE_HERE = (function () { var _private_obj_name_here = { // settings […]
Javascript: automatic type convertion
if(something){ // Do this } Do this will happen, if “something” is equal to: undefined null 0 (as in the number) “” (empty string) NaN (not a number object) Be careful! Any kind of object, or even the string “0” will evaluate to true! The reason stuff like this works: var myvar = me || […]
Google Maps: some of the things you can do with it
http://jsfiddle.net/ramiror/fTtUA/ remove markers: my_marker.setMap(null) Programatically create a dialog box: $.mobile.changePage( “page for the dialog content”, { role: “dialog”} );
javascript: when scope inside setTimeOut is giving you headaches
Sometimes you have to setTimeOut call a function, but you need to provide the current scope for the function to execute as well. Say for example, you want to call this.myFunction() with (this) { setTimeout( function() { myFunction() }, 1000 );} Doing the with(this) allows you to pass “this” scope, so myFunction will get executed […]
Javascript basics: script tag
Attributes: type: The language your script is in. Defaults to javascript since HTML5. In older browsers you were able to run alt languages, like VBScript in IE. In fact, it is supposed to actually point to the mime type of your script. The syntax is, for example: type=”text/javascript” language: not used. Deprecated. async: When set to […]
Backbone.js common problems
http://readystate4.com/2011/10/22/common-backbone-errors-and-what-they-mean/
Javascript patterns: namespacing your objects.
Avoid clustering the global scope. Start with uppercase (recommended): var MYAPP = MYAPP || {}; To be able to construct objects with several levels of deepness, use the following utility function: var UTILITIES = UTILITIES || {}; UTILITIES.namespace = function (ns_string) { var parts = ns_string.split(‘.’), parent = MYAPP, i; // strip redundant leading global […]
Javascript and scrollbars
To detect if the scrollbar is all the way to the top: var scrollY = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; if(scrollY == 0){ /* Scroll at top, do something here */ } Infinite scroll implementation: $(window).scroll(function(){ if(($(window).scrollTop() + 100) > ($(document).height() – $(window).height())){ /* You are 100px close to the bottom scroll, load more content […]