JavaScript Basics

In JavaScript, where functions are first-class objects that aren’t declared as part of anything, the object referenced by this (the function context) is determined not by how the function is declared but by how it’s invoked.
In JavaScript this always refers to the owner (caller) of the function we're executing, or rather, to the object that a function is a method of.
l When the function is called directly as a top-level function, the function context is the window instance.
l When called as a property of an object, the object becomes the function context.
We can also explicitly set function context by call() or apply() methods. the function context would be whatever object is passed as the first parameter.
The difference between these two methods is that we pass function parameters as an array in apply() method, but pass them one by one in call() method.
Variable Scope
Global JavaScript Variables
Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it.
Global variables are destroyed when you close the page.
Local JavaScript Variables
A variable declared within function with var keyword becomes LOCAL and can only be accessed within that function.
Any variable that is declared inside a function using the var keyword will have a local scope.
If a variable is declared without the var keyword, even in a function, it will have a global scope.
If you assign values to variables that have not yet been declared, the variables will automatically be declared as global variables.
Browser event models
DOM Level 0 Event Model - Netscape Event Model
Event handlers are declared by assigning a reference to a function instance to properties of the DOM elements.
EVENT BUBBLING
The event-handling mechanism of the browser checks to see if a handler has been established for this event, then the event model checks whteher its parent and its parent's parent has established a handler for this event type, all the way up to the top of the DOM tree.
Prevent bubbling
Set event cancelBubble property to true
Prevent event default action
At the end of handler, return false to prevent event default action.
Disadvantages
Because a property is used to store a reference to a function that’s to serve as an event handler, only one event handler per element can be registered for any specific event type at a time.
The DOM Level 2 Event Model
DOM Level 2 event handlers (listeners) are established via an element method.
addEventListener(eventType,listener,useCapture)
EVENT PROPAGATION - capture then bubble
Under the DOM Level 2 Event Model, when an event is triggered, the event first propagates from the root of the DOM tree down to the target element and then propagates again from the target element up to the DOM root. The former phase (root to target) is called capture phase, and the latter (target to root) is called bubble phase.
Ancient inline model



The equality (==) operator compare for equality after doing necessary type casting
the identity operator (===) doesn't do any conversions.
- always use strict equality

Post a Comment

Labels

Java (159) Lucene-Solr (112) Interview (61) All (58) J2SE (53) Algorithm (45) Soft Skills (38) Eclipse (33) Code Example (31) Linux (25) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) J2EE (13) Network (13) Troubleshooting (13) PowerShell (11) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) Problem Solving (9) UIMA (9) html (9) Http Client (8) Maven (8) Security (8) bat (8) blogger (8) Big Data (7) Continuous Integration (7) Google (7) Guava (7) JSON (7) Shell (7) ANT (6) Coding Skills (6) Database (6) Lesson Learned (6) Programmer Skills (6) Scala (6) Tips (6) css (6) Algorithm Series (5) Cache (5) Dynamic Languages (5) IDE (5) System Design (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Firefox (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Python (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts