{"id":697,"date":"2011-02-01T16:11:05","date_gmt":"2011-02-01T15:11:05","guid":{"rendered":"http:\/\/www.dotmana.com\/index.php\/?p=697"},"modified":"2011-02-01T16:11:40","modified_gmt":"2011-02-01T15:11:40","slug":"javascript-en-10-minutes","status":"publish","type":"post","link":"https:\/\/www.dotmana.com\/weblog\/2011\/02\/javascript-en-10-minutes\/","title":{"rendered":"Javascript en 10 minutes"},"content":{"rendered":"<p><a href=\"http:\/\/javascript.infogami.com\/Javascript_in_Ten_Minutes\" target=\"_blank\">Article original ici<\/a>, je me permets de le sauvegarder en le copiant ici.<\/p>\n<blockquote>\n<table id=\"main\" border=\"0\">\n<tbody>\n<tr>\n<td id=\"left\"><\/td>\n<td id=\"body\">\n<h1><a href=\"http:\/\/javascript.infogami.com\/\">Javascript<\/a><\/h1>\n<p>(ECMAScript)<\/p>\n<h2>Javascript in Ten Minutes<\/h2>\n<div>\n<h2>Breakdown&#8230;<\/h2>\n<h3>Basic Types<\/h3>\n<pre>var intValue = 1;\r\nvar floatValue = 3.0;\r\nvar stringValue = \"This is a string\\n\";\r\nvar sqString = 'This is also a string';<\/pre>\n<p>Javascript is a dynamically typed language.  Variables are declared with the keyword <tt>var<\/tt>.  Common simple types are supported.<\/p>\n<h3>Arrays<\/h3>\n<pre>var emptyList = [];\r\nvar homogenousList = [1, 2, 3];\r\nvar heterogenousList = [\"one\", 2, 3.0];<\/pre>\n<p>Javascript has built-in collection objects.  The Array object is a dynamically typed sequence of Javascript values.  They are  created with the bracket notation <tt>[]<\/tt> or with the <tt>new<\/tt> operator on the <tt>Array<\/tt> object (e.g.  <tt>new Array(5)<\/tt>).<\/p>\n<h3>Property Maps<\/h3>\n<pre>var emptyMap = {};\r\nvar homogenousMap = {\"one\": 1, \"two\": 2, \"three\": 3};\r\nvar heterogenousMap = {\"one\": 1,\r\n                       \"two\": \"two\",\r\n                       \"three\": 3.0};<\/pre>\n<p>Along with <tt>Array<\/tt>s are the <tt>Object<\/tt> objects. They act as property maps with strings serving as keys to dynamically typed data.<\/p>\n<h4>Access<\/h4>\n<pre>\/\/ Dot notation property access\r\nwindow.alert(\"Homogenous map property \\\"one\\\" \"\r\n             + <span style=\"color: blue;\">homogenousMap.one<\/span>);\r\n\/\/ Subscript notation property access\r\nwindow.alert(\"Homogenous map property \\\"two\\\" \"\r\n             + <span style=\"color: blue;\">homogenousMap[\"two\"]<\/span>);<\/pre>\n<h4>Assignment<\/h4>\n<pre>homogenousMap[\"one\"] = 10;\r\nhomogenousMap.two = 20;<\/pre>\n<h4>Removal<\/h4>\n<pre>delete homogenousMap[\"one\"];\r\ndelete homogenousMap.two;<\/pre>\n<h4>Iteration<\/h4>\n<pre>for (var <span style=\"color: green;\">key<\/span> in <span style=\"color: blue;\">heterogenousMap<\/span>) {\r\n    window.alert(\"Heterogenous map property \\\"\"\r\n                 + <span style=\"color: green;\">key<\/span>\r\n                 + \"\\\" = \"\r\n                 + <span style=\"color: blue;\">heterogenousMap[<span style=\"color: green;\">key<\/span>]<\/span>);\r\n}<\/pre>\n<h3>Functions<\/h3>\n<pre>var callable = function (message) { \/\/ &lt;-- notice assignment\r\n    window.alert(\"Callable called with message = \"\r\n                 + message);\r\n}\r\n\r\nfunction createClosure(initial) {\r\n    var res = function () {\r\n        initial = initial + 1;\r\n        window.alert(\"Closure with modified state \"\r\n                     + initial);\r\n    }\r\n    return res;\r\n}\r\n\r\nfunction callCallable(f, x) {\r\n    f(x);\r\n}\r\n\r\nfunction composeCallables(f, g, x) {\r\n    f(g(x));\r\n}<\/pre>\n<p>Functions are first-class objects.  That means that they can be created dynamically, stored, passed and returned  just like any other value.<\/p>\n<h3>Objects<\/h3>\n<pre>function MyObject(name, value) {\r\n    this.name = name;\r\n    this.value = value;\r\n}<\/pre>\n<p>Javascript supports prototype based object orientation. Not a class type but an object constructor is created for new objects with particular properties.  In the example above the <tt>this<\/tt> keyword used to  reference the &#8221;current instance&#8221; of the object. The <tt>this<\/tt> object is essentially a property map with members accessed (and initialized) in this  example with the dot notation.<\/p>\n<p>The object constructor, <tt>MyObject<\/tt>, is an object constructor not in how it&#8217;s defined, which looks like any other Javascript function, but in  how it&#8217;s &#8221;invoked&#8221;.<\/p>\n<pre>    var my = new MyObject(\"foo\", 5);<\/pre>\n<p>The <tt>new<\/tt> operator before the function  invokes the function with a newly construced object as <tt>this<\/tt> and returns that the initialized object.<\/p>\n<h4>Object Prototype<\/h4>\n<p>Part of what makes a language object oriented is that data not only has properties but also &#8221;behaviors&#8221;. Also known as: member functions; methods; and object  messages. To implement a member function in Javascript one would be tempted to write something like what&#8217;s below based on the member initialization exampled above.<\/p>\n<pre><span style=\"color: red;\">function BadObject(data) {\r\n    this.data = data\r\n    this.memberFunction = function () {\r\n        \/\/ ...functions on data...\r\n    }\r\n}\r\n<\/span><\/pre>\n<p>While the code above will work without error, it does create a new closure for each member function for each new instance of the object.  What&#8217;s really required is a class level function that works on instance data. But remember, Javascript objects aren&#8217;t class based but prototype based.  So how do we implement &#8220;class&#8221; level member functions? (<a href=\"http:\/\/javascript.infogami.com\/#Member_Function_Implementation\">Skip to Implementation<\/a>) Better yet, how do we implement &#8220;class&#8221; level members functions in general?<\/p>\n<p>Enter the <tt>prototype<\/tt> member.<\/p>\n<p>The internal object member, <tt>prototype<\/tt>, has  language defined significance in that it is used for  resolving property names if the property isn&#8217;t found in the current property map.  It&#8217;s considered internal because, while the instance&#8217;s <tt>prototype<\/tt> member is &#8221;inherited&#8221; from the &#8221;constructor&#8217;s&#8221; <tt>prototype<\/tt> member, it cannot be accessed directly from the object instance itself.  The defined <tt>prototype<\/tt> member  is a property map itself which holds members for property  name resolution. Consider the example below:<\/p>\n<pre> var parentPropertyMap = {<span style=\"color: red;\">\"bar\"<\/span>: <span style=\"color: red;\">\"I'm the bar\"<\/span>};\r\n\r\n <span style=\"color: grey;\">\/\/ Define the constructor with inheritable properties<\/span>\r\n function ChildObject(foo) {\r\n     this.<span style=\"color: blue;\">foo<\/span> = foo;\r\n }\r\n ChildObject.prototype = parentPropertyMap;\r\n\r\n childPropertyMap1 = new ChildObject(<span style=\"color: blue;\">\"I'm the foo1\"<\/span>);\r\n childPropertyMap2 = new ChildObject(<span style=\"color: blue;\">\"I'm the foo2\"<\/span>);\r\n\r\n <span style=\"color: grey;\">\/\/ Prints \"childPropertyMap1.foo = I'm the foo1\"<\/span>\r\n window.alert(\"childPropertyMap1.foo = \" + childPropertyMap1.<span style=\"color: blue;\">foo<\/span>);\r\n\r\n <span style=\"color: grey;\">\/\/ Prints \"childPropertyMap2.foo = I'm the foo2\"<\/span>\r\n window.alert(\"childPropertyMap2.foo = \" + childPropertyMap2.<span style=\"color: blue;\">foo<\/span>);\r\n\r\n <span style=\"color: grey;\">\/\/ Prints \"childPropertyMap1.bar = I'm the bar\"<\/span>\r\n window.alert(\"childPropertyMap1.bar = \" + childPropertyMap1.<span style=\"color: red;\">bar<\/span>);\r\n\r\n <span style=\"color: grey;\">\/\/ Prints \"childPropertyMap2.bar = I'm the bar\"<\/span>\r\n window.alert(\"childPropertyMap2.bar = \" + childPropertyMap2.<span style=\"color: red;\">bar<\/span>);<\/pre>\n<p>The member <tt>foo<\/tt> is an instance member added to the instance&#8217;s property map during construction:<\/p>\n<pre> function ChildObject(foo) {\r\n     <span style=\"color: blue;\">this.foo = foo;<\/span>\r\n }<\/pre>\n<p>while <tt>bar<\/tt> is in the constructor&#8217;s <tt>prototype<\/tt>:<\/p>\n<pre> var parentPropertyMap = {\"bar\": \"I'm the bar\"};\r\n ...\r\n ChildObject.prototype = parentPropertyMap;<\/pre>\n<p>which is &#8221;inherited&#8221; during the <tt>new<\/tt> operation:<\/p>\n<pre> childPropertyMap1 = new ChildObject(\"I'm the foo1\");\r\n childPropertyMap2 = new ChildObject(\"I'm the foo2\");<\/pre>\n<p>In other words, the member, <tt>bar<\/tt>, is shared across  all instances of <tt>ChildObject<\/tt>.<\/p>\n<p>Therefore, by implementing the <tt>prototype<\/tt> member of the  constructor function, we can think of the constructor function itself as the &#8220;class&#8221; object.  Complete with static class  functions:<\/p>\n<pre> function ClassObject() {}\r\n ClassObject.staticClassFunction = function(x) {\r\n     return x * 2;\r\n }<\/pre>\n<p>static class variables:<\/p>\n<pre> function ClassObject() {}\r\n ClassObject.staticClassVariable = 5;<\/pre>\n<p>shared member variables:<\/p>\n<pre> function ClassObject() {}\r\n ClassObject.prototype.sharedMember = 5;<\/pre>\n<p>and of course, shared member functions:<\/p>\n<pre> function ClassObject(x) {\r\n     this.x = x;\r\n }\r\n ClassObject.prototype.memberFunction = function(x) {\r\n     return x * this.x;\r\n }<\/pre>\n<h4>Member Function Implementation<\/h4>\n<pre>function Message(message) {\r\n    this.message = message;\r\n}\r\n\r\nMessage.prototype.show = function() {\r\n    window.alert(\"Message.show() with message = \"\r\n                 + this.message);\r\n}<\/pre>\n<p>(<a href=\"http:\/\/javascript.infogami.com\/Javascript_Objects\">More on Classes and Objects<\/a>)<\/p>\n<h2>Example Code<\/h2>\n<pre>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Basic Types\r\nvar intValue = 1;\r\nvar floatValue = 3.0;\r\nvar stringValue = \"This is a string\\n\";\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Array\r\nvar emptyList = [];\r\nvar homogenousList = [1, 2, 3];\r\nvar heterogenousList = [\"one\", 2, 3.0];\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Property Map\r\n\/\/\r\nvar emptyMap = {};\r\nvar homogenousMap = {\"one\": 1, \"two\": 2, \"three\": 3};\r\nvar heterogenousMap = {\"one\": 1,\r\n                       \"two\": \"two\",\r\n                       \"three\": 3.0};\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Functions as values\r\n\/\/\r\nvar callable = function (message) { \/\/ &lt;-- notice assignment\r\n    window.alert(\"Callable called with message = \"\r\n                 + message);\r\n}\r\n\r\nfunction createClosure(initial) {\r\n    var res = function () {\r\n        initial = initial + 1;\r\n        window.alert(\"Closure with modified state \"\r\n                     + initial);\r\n    }\r\n    return res;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Functions as arguments\r\n\/\/\r\nfunction callCallable(f, x) {\r\n    f(x);\r\n}\r\n\r\nfunction composeCallables(f, g, x) {\r\n    f(g(x));\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Objects\r\n\/\/\r\nfunction MyObject(name, value) {\r\n    this.name = name;\r\n    this.value = value;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Objects with Member Functions\r\n\/\/\r\nfunction Message(message) {\r\n    this.message = message;\r\n}\r\n\r\nMessage.prototype.show = function() {\r\n    window.alert(\"Message.show() with message = \"\r\n                 + this.message);\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Demo Utilities\r\n\/\/\r\nfunction quote(message) {\r\n    return \"\\\"\" + message + \"\\\"\";\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ HTML Invoked demonstration\r\n\/\/\r\n\/\/\r\nfunction main() {\r\n    window.alert(\"Integer = \" + intValue);\r\n    window.alert(\"Float = \" + floatValue);\r\n    window.alert(\"String = \" + stringValue);\r\n\r\n    for (var item in emptyList) {\r\n        window.alert(\"Empty list item = \" + item);\r\n    }\r\n\r\n    \/\/ Script style index iteration\r\n    for (var i in homogenousList) {\r\n        window.alert(\"Homogenous list item = \"\r\n                     + homogenousList[i]);\r\n    }\r\n\r\n    \/\/ C style index iteration\r\n    for (var i=0; i &lt; heterogenousList.length; ++i) {\r\n        window.alert(\"Heterogenous list item = \"\r\n                     + heterogenousList[i]);\r\n    }\r\n\r\n    \/\/ Dot notation property access\r\n    window.alert(\"Homogenous map property \\\"one\\\" \"\r\n                 + homogenousMap.one);\r\n    \/\/ Subscript notation property access\r\n    window.alert(\"Homogenous map property \\\"two\\\" \"\r\n                 + homogenousMap[\"two\"]);\r\n\r\n    for (var key in heterogenousMap) {\r\n        window.alert(\"Heterogenous map property \\\"\"\r\n                     + key\r\n                     + \"\\\" = \"\r\n                     + heterogenousMap[key]);\r\n    }\r\n\r\n    callable(\"(Function value invoked)\");\r\n    closure();\r\n    closure();\r\n\r\n    callCallable(closure);\r\n    composeCallables(callable, quote, \"My Message\");\r\n\r\n    var my = new MyObject(\"foo\", 5);\r\n    window.alert(\"MyObject my.name = \" + my.name);\r\n    window.alert(\"MyObject my[\\\"value\\\"] = \" + my[\"value\"]);\r\n\r\n    var msg = new Message(\"bar\");\r\n    for (var key in Message.prototype) {\r\n        window.alert(\"Message prototype member \\\"\"\r\n                     + key\r\n                     + \"\\\" = \"\r\n                     + Message.prototype[key]);\r\n    }\r\n\r\n    window.alert(\"Message msg.message = \" + msg.message);\r\n    msg.show();\r\n}<\/pre>\n<p><a href=\"http:\/\/javascript.infogami.com\/Javascript_in_Ten_Minutes\"><\/a><\/div>\n<\/td>\n<td id=\"sidebar\"><a href=\"http:\/\/javascript.infogami.com\/_account\/in?path=\/Javascript_in_Ten_Minutes\"><br \/>\n<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Article original ici, je me permets de le sauvegarder en le copiant ici. Javascript (ECMAScript) Javascript in Ten Minutes Breakdown&#8230; Basic Types var intValue = 1; var floatValue = 3.0; var stringValue = &#8220;This is a string\\n&#8221;; var sqString = &hellip; <a href=\"https:\/\/www.dotmana.com\/weblog\/2011\/02\/javascript-en-10-minutes\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-697","post","type-post","status-publish","format-standard","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":2,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/697\/revisions\/699"}],"wp:attachment":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}