JavaScript

From HaFrWiki
Revision as of 13:06, 26 October 2018 by Hjmf (talk | contribs) (See also)
Jump to: navigation, search

Always I can not find a pleasant and good reference/example of JavaScript code [1], but constant searching helps [2].

Documentation

Not a beginners guide and note a reference. JavaScript Garden (Bonsai Garden) is a growing collection of documentation about the most quirky parts of the JavaScript programming language. It gives advice to avoid common mistakes and subtle bugs, as well as performance issues and bad practices, that non-expert JavaScript programmers may encounter on their endeavours into the depths of the language. [2]

JavaScript Garden does not aim to teach you JavaScript. Former knowledge of the language is strongly recommended in order to understand the topics covered in this guide. In order to learn the basics of the language, please head over to the excellent guide on the Mozilla Developer Network.

Cookies

See for a description on how to cope with JavaScript Cookies

FAQ

My personal list of FAQ's.

Javascript function Name

How to het the function name of the function you are in. [3]

  • Use: arguments.callee.name
  • Example (code snippet):

<syntaxhighlight lang="javascript"> var logFormatLBW = '[%-25.25s] - %-15s %s';

function getTail() {

 ...
 m = arguments.callee.name; 
 console.log(sprintf( logFormatLBW, m, 'callee'  , arguments.callee.name));

</syntaxhighlight>

Default parameter

Is it possible to have a function with a default parameter?

  • Yes, see example.

<syntaxhighlight lang="javascript"> /** Does something foo... @param string optionalParameter [Optional] option for .... [default: ""]

  • /

function foo( optionalParameter ) {

  var option = typeof optionalParameter !== 'undefined' ? optionalParameter : "";

} </syntaxhighlight>

Check existence of variable

Can you check on the existence of a variable/object?

  • Yes, see example

<syntaxhighlight lang="javascript"> if ( somecondition) {

   var newObject = { name: "myname", ... };

}

if ( typeof newObject !== 'undefined') {

  newObject.method();

} </syntaxhighlight> The above will check whether foo was actually declared or not; just referencing it would result in a ReferenceError. This is the only thing typeof is actually useful for.

Links

Redirector

use the lcation object. An example:

<html>
<title>CodeAve.com (JavaScript: Current Page URL)</title>
<body bgcolor="#FFFFFF">
...
<script language="JavaScript">
<!-- 
  document.write (location.href);
  document.write ("<p><a href='" +location.href + "'>Link To Current Page</a></p>");
// -->
</script>
...
</body>
</html>

Javascript asynchronous

The asynchronous behaviour of Javascript is well known, but also not very understood by all the developers and users.
Therefore a small explanation with examples to describe the different opportunities and possibilities.

Assuming the following problem:

Synchronous Asynchronous

<syntaxhighlight lang="javascript" line start="1"> function findItem() {

   var item;
   while(item_not_found) {
       // search
   }
   return item;

}

var item = findItem();

// Do something with item doSomethingElse(); </syntaxhighlight> Even though findItem might take a long time to execute, any code coming after var item = findItem(); has to wait until the function returns the result.

<syntaxhighlight lang="javascript" line start="1"> findItem(function(item) {

   // Do something with item

}); doSomethingElse(); </syntaxhighlight>

Callback

Promises or Futures

==== with

See also

Reference

top

  1. JavaScriptKit, Index, Tutorials and Reference. This is a very good start.
  2. 2.0 2.1 Bonsai Garden, Javascript Examples, Best practices and tutorials.
  3. Kangax, Named function expressions demystified.