diff options
Diffstat (limited to 'javascript.html.markdown')
| -rw-r--r-- | javascript.html.markdown | 47 | 
1 files changed, 35 insertions, 12 deletions
| diff --git a/javascript.html.markdown b/javascript.html.markdown index 85c8a52d..e7066291 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -103,7 +103,7 @@ false;  // ... which works with more than just strings  "1, 2, " + 3; // = "1, 2, 3" -"Hello " + ["world", "!"] // = "Hello world,!" +"Hello " + ["world", "!"]; // = "Hello world,!"  // and are compared with < and >  "a" < "b"; // = true @@ -180,6 +180,24 @@ myArray.length; // = 4  // Add/Modify at specific index  myArray[3] = "Hello"; +// Add and remove element from front or back end of an array +myArray.unshift(3); // Add as the first element +someVar = myArray.shift(); // Remove first element and return it +myArray.push(3); // Add as the last element +someVar = myArray.pop(); // Remove last element and return it + +// Join all elements of an array with semicolon +var myArray0 = [32,false,"js",12,56,90]; +myArray0.join(";") // = "32;false;js;12;56;90" + +// Get subarray of elements from index 1 (include) to 4 (exclude) +myArray0.slice(1,4); // = [false,"js",12] + +// Remove 4 elements starting from index 2, and insert there strings +// "hi","wr" and "ld"; return removed subarray +myArray0.splice(2,4,"hi","wr","ld"); // = ["js",12,56,90] +// myArray0 === [32,false,"hi","wr","ld"] +  // JavaScript's objects are equivalent to "dictionaries" or "maps" in other  // languages: an unordered collection of key-value pairs.  var myObj = {key1: "Hello", key2: "World"}; @@ -222,7 +240,7 @@ while (true){  var input;  do {      input = getInput(); -} while (!isValid(input)) +} while (!isValid(input));  // The `for` loop is the same as C and Java:  // initialization; continue condition; iteration. @@ -293,7 +311,7 @@ myFunction("foo"); // = "FOO"  // automatic semicolon insertion. Watch out for this when using Allman style.  function myFunction(){      return // <- semicolon automatically inserted here -    {thisIsAn: 'object literal'} +    {thisIsAn: 'object literal'};  }  myFunction(); // = undefined @@ -388,7 +406,7 @@ myFunc(); // = undefined  // through `this`, even if it wasn't attached when it was defined.  var myOtherFunc = function(){      return this.myString.toUpperCase(); -} +};  myObj.myOtherFunc = myOtherFunc;  myObj.myOtherFunc(); // = "HELLO WORLD!" @@ -397,7 +415,7 @@ myObj.myOtherFunc(); // = "HELLO WORLD!"  var anotherFunc = function(s){      return this.myString + s; -} +};  anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"  // The `apply` function is nearly identical, but takes an array for an argument @@ -420,7 +438,7 @@ boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"  // `bind` can also be used to partially apply (curry) a function. -var product = function(a, b){ return a * b; } +var product = function(a, b){ return a * b; };  var doubler = product.bind(this, 2);  doubler(8); // = 16 @@ -430,11 +448,11 @@ doubler(8); // = 16  var MyConstructor = function(){      this.myNumber = 5; -} +};  myNewObj = new MyConstructor(); // = {myNumber: 5}  myNewObj.myNumber; // = 5 -// Unlike most other popular object-oriented languages, JavaScript has no  +// Unlike most other popular object-oriented languages, JavaScript has no  // concept of 'instances' created from 'class' blueprints; instead, JavaScript  // combines instantiation and inheritance into a single concept: a 'prototype'. @@ -451,7 +469,7 @@ var myObj = {  var myPrototype = {      meaningOfLife: 42,      myFunc: function(){ -        return this.myString.toLowerCase() +        return this.myString.toLowerCase();      }  }; @@ -515,7 +533,7 @@ MyConstructor.prototype = {  };  var myNewObj2 = new MyConstructor();  myNewObj2.getMyNumber(); // = 5 -myNewObj2.myNumber = 6 +myNewObj2.myNumber = 6;  myNewObj2.getMyNumber(); // = 6  // Built-in types like strings and numbers also have constructors that create @@ -540,7 +558,7 @@ if (new Number(0)){  // you can actually add functionality to a string, for instance.  String.prototype.firstCharacter = function(){      return this.charAt(0); -} +};  "abc".firstCharacter(); // = "a"  // This fact is often used in "polyfilling", which is implementing newer @@ -556,7 +574,7 @@ if (Object.create === undefined){ // don't overwrite it if it exists          Constructor.prototype = proto;          // then use it to create a new, appropriately-prototyped object          return new Constructor(); -    } +    };  }  ``` @@ -589,6 +607,10 @@ some of the more complicated examples.  [Javascript: The Right Way][10] is a guide intended to introduce new developers  to JavaScript and help experienced developers learn more about its best practices. +[Javascript:Info][11] is a modern javascript tutorial covering the basics (core language and working with a browser) +as well as advanced topics with concise explanations. + +  In addition to direct contributors to this article, some content is adapted from  Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the  Mozilla Developer Network. @@ -604,3 +626,4 @@ Mozilla Developer Network.  [8]: http://eloquentjavascript.net/  [9]: http://watchandcode.com/courses/eloquent-javascript-the-annotated-version  [10]: http://jstherightway.org/ +[11]: https://javascript.info/ | 
