Posts

Showing posts with the label JavaScript

JavaScript Tips: Module Pattern

Coming from the Java background, my main problem with JavaScript is encapsulation: global space is not good. I love OO because object is the best place to encapsulate the data. Here are the patterns I would like to follow always. In the JavaScript, function can be used as : function method constructors classes Module As a first example, will start with the Module. Module Pattern In this pattern, you can define number of privilege method which have access to the secret information such as private variables and methods . For example, setFirstName method can set the name local variable as well as the function getUserFullName has access to the private method getFullName as shown int he following code: var person = (function (firstName, lastName, age){ //properties var a = age; var fname = firstName; var lname = lastName; //common functions function getFullName(f,l){ return fname+" "+lname; } return { //open to outside getFirstName:...

JavaScript Notes

Coming from the Java background, my main problem with JavaScript is encapsulation: global space is not good. I love OO because object is the best place to encapsulate the data. Here are the patterns I would like to follow always. In the JavaScript, function can be used as : function method constructors classes Module As a first example, will start with the Module. Module Pattern In this pattern, you can define number of privilege method which have access to the secret information such as private variables and methods . For example, setFirstName method can set the name local variable as well as the function getUserFullName has access to the private method getFullName as shown int he following code: var person = ( function (firstName, lastName, age) { //properties var a = age; var fname = firstName; var lname = lastName; //common functions function getFullName (f,l) { return fname+ " " +lname; } return { //open to outside ...

JavaScript 1: history object

The top most object in the Browser Object Model (BOM) is the “window”. I have three files history.html:  Start file1.html: Middle file2.html: Last In the Start, you can click the “go to middle” to visit the page file1.html. In this page you have two buttons, when you click “Back” it should take you back to the history.html, otherwise you can go to the last page click by “go to end”. In the “End” page you can go to the page entering -2 for “Start” or –1 for “Middle”. <!DOCTYPE html> <html> <head> </head> <body> <h1>Start</h1> <a href="file1.html">go to middle</a> </body> </html> As shown in the above code, there is just one link to go to the middle page. <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1> In the Middle, one to go</h1> <a href="file2.html">go to end</a> <input type=...