Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  trim() in JavaScript

In the past, JavaScript didn't define the trim() function for String object. This makes web programmers frustrated since they often need to implement this function themselves if they want to handle user inputs. The new version of ECMA-262 introduces the trim() function.15.5.4.20   String.prototype.trim ( )     The following steps are taken:     1.   Call CheckObjectCoercible passing the this value as its argument.   2.   Let S be the result of calling ToString, giving it the this value as its argument.   3.  L...

3,743 0       JAVASCRIPT IMPLEMENTATION TRIM()


  Some short code snippets which use all JavaScript keywords

In JavaScript, keywords are reserved and cannot be used as variable or function names. For example void, function and this are JavaScript keywords, they are keywords which are reserved for special use. Here is list of the keywords in JavaScript. We also show you some short code snippets which use all the keywords in JavaScript.void function() {//abcd   do break;while(typeof delete this);  for(var a;;)  if (true)  with(null)  try{}catch(a){}finally{} else throw new 1;   switch(1 in 1 instanceof 1) {    case false:    default: ...

4,188 0       JAVASCRIPT KEYWORD CODE SNIPPET


  Three ways to define class in JavaScript

In object oriented programming, class is the template of object, it defines the properties and methods of a group of objects. Unfortunately, JavaScript doesn't support class, but we can have some workarounds to simulate class.1. Constructor functionThis is the classical approach, it is also the approach mentioned in many text books. It uses the constructor function to simulate class and it uses the keyword this to represent the object internally.function Cat() {  this.name = "Kelly";}when creating the instance, use the new keyword.var cat1=new Cat();alert(cat1.name); //KellyThe p...

11,803 0       JAVASCRIPT METHOD CLASS INHERITANCE PRIVATE


  Regular expression to get html meta description

When we need to process a HTML page source code, we often need to retrieve the meta description of the page besides the links in the page. This description is usually located in <meta> tag of a HTML page. The meta description is very useful for search engine index. How can we retrieve the meta description? If we use a regular expression, we can easily get the meta description.In JavaScript, the regular expression looks like :var pattern = /<meta.*?name="description".*?content="(.*?)".*?>|<meta.*?content="(.*?)".*?name="description".*?>/i;since the description is the content i...

16,121 0       JAVASCRIPT HTML REGULAR EXPRESSION META DESCRIPTION


  Get hostname from a URL using JavaScript

Sometimes we may have strings which contain some UR;s and we may want to retrieve the hostname from the URLs for some statistic use. For example, we may have a URL : http://www.example.com/aboutus.html. We may want to retrieve the www.example.com from the URL. How? Use regular expression. Here I give an example using JavaScript. If you want to check whether a string is a URL or not. Refer to Detect URLs in a Block of Text. In JavaScript, we can have a regular expression like var pattern=/(.+:\/\/)?([^\/]+)(\/.*)*/i;the regular expression pattern can be used to get the hostname. There are three...

18,528 0       JAVASCRIPT REGULAR EXPRESSION URL HOSTNAME


  var in JavaScript

Geoff published an article sometime ago--"How one missing var ruined our launch". This article described one case where MelonCard uses Node.js as their backend system, suddenly there was a small registration peak period--50-100 people registered concurrently, then the entire website stopped responding and many other strange problems showed up. When they were investigating the source of the problem, they found one missing var in the following code.app.all(‘/apps/:user_id/status’, function(req, res, next) {    // …    initial = extractVa...

3,366 0       JAVASCRIPT VARIABLE SCOPE


  Create namespace in JavaScript

A namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its def...

5,591 0       JAVASCRIPT EXAMPLE NAMESPACE


  this in JavaScript

this is a keyword in JavaScript. It refers to an internal object created automatically when a function executes, it can only be used in a function. For example:        function test(){    this.x = 1;  }The this keyword will change when a function is called in different situations. However, the general rule is : this refers to the object which calls the function.Next we discuss the use of this in 4 different situations.1. In pure function callThis is the most common use of a function, it is a global call, so the this w...

5,748 0       JAVASCRIPT THIS KEYWORD USE