Hidden Features of JavaScript

Java Script Hidden Function

A couple of things, first I cleaned up your JS a bit and encapsulated your null check in a method. I also added the missing markup elements which I assume were omitted on accident from your OP.

Here is a fiddle: http://jsfiddle.net/gev4kwnf/5/

The JavaScript now looks like this:

function setVisible(id, visible) {
var o = document.getElementById(id); //get the element based on it's id

//if the element exists then set it's visiblity
if (isElement(o)) {
o.style.visibility = visible ? 'visible' : 'hidden';
} else {
alert("Element with id '" + id + "' not found.");
}
}

function setDisplay(id, visible) {
var o = document.getElementById(id);
if (isElement(o)) {
o.style.display = visible ? 'block' : 'none';
} else {
alert("Element with id '" + id + "' not found.");
}
}

function isElement(obj) {
var returnValue = false;
if (obj == undefined || obj == null) {
returnValue = false;
} else {
returnValue = true;
}
return returnValue;
}

Javascript FAB framework on Node.js

Is plain JavaScript, it is a function chaining pattern.

The first line, ( fab = require("fab") ) includes the fab function and returns a reference to it.

All the subsequent parentheses are function calls, each function invocation returns probably the same function again and again.

The pattern probably looks like this simplified example:

var foo = function (arg) {
// detect what the argument is
if (typeof arg == 'function') {
// do something with arg
console.log('function: '+arg());
} else if (arg instanceof RegExp) {
// arg is a RegExp...
console.log('A RegExp: '+arg);
} else if (typeof arg == "string") {
// arg is a string
console.log('A string: '+arg);
}
return foo; // return a reference to itself
};

(foo)
(function() { return "Foo "; })
(/bar/)
(" baz!");

Outputs:


function: Foo
A RegExp: /bar/
A string: baz!

Features of JavaScript that C developers fail to take advantage of?

It's mainly an issue with writing procedural code in JavaScript. You'll miss out on two big features if you write JavaScript like C :

Prototypical OOP and First class functions

Prototypical OOP allows you to create new objects and apply many of the OOP patterns of other languages. It's rather different from classical OOP.

A good article on that would be JavaScript Garden Objects section

First class functions allow you to pass functions as arguments and this allows you to write functional code. With just this alone you can get close to the power of LISP, ML or Haskell. Again a good article is JavaScript Garden Functions section.

The reason why these two things are big is because they allow you to use the OOP paradigm and the Functional paradigm.

Otherwise you will be stuck writing Procedural JavaScript forever.



Related Topics



Leave a reply



Submit