JavaScript mode Reading Notes Chapter 1 literal and constructor

Source: Internet
Author: User

JavaScript mode Reading Notes Chapter 1 literal and constructor
1. Object literal volume-1. The custom object created in Javascript is variable in the task. You can add functions as needed from an empty object. The object literal mode allows us to add functions to an object when creating it.

      <script>// Define an empty objectvar dog = {};// Add an empty objectdog.name = "Liubo";// Add method to objectdog.getName = function(){return dog.name;};// Change attributes and Methodsdog.getName = function(){returen "LaoLiu";};// Delete all attributes/methodsdelete dog.name;  </script>
<script>var dog = {name: "LaoLiu",getName:function(){return this.name;}};  </script>
-2. Object literal syntax a. Wrap the object in braces. B. Separate attributes and methods by commas. C. Use a colon to separate the attribute name and attribute value. D. When the copy is changed, it will end with right brackets.

2. Custom constructor:
<script>var Person = function(name){this.name = name;this.say = function(){return "I am " + this.name;}}var adam = new Person("Adam");console.log(adam.say());  </script>

When the constructor is called using the new operator:
A. Create an empty object, and the this variable references this object. It also inherits the prototype of this function.
B. attributes and methods are added to the object referenced by this.
C. The newly created object is referenced by this and the result is implicitly returned.
The above code is executed in the background as follows:
var Person = function(name){var this  = {};this.name = name;this.say = function(){return "I am " + this.name};//return this;};
3. the return value of the constructor.
  <script>var Objectmark = function(){this.name = "This is it!";var that = {};that.name = "And that is that!";Return that; // The name initialized for the first time will be ignored because of return that.};var o = new Objectmark();console.log(o.name);  </script>
4. Force the new Mode
<script> function Man(){this.name = "Lao Liu"; }// Create a new object var laoLiu = new Man(); console.log(typeof laoLiu);// object console.log(laoLiu.name);//Lao Liu// Reverse mode// The new operation is not used. var pangLiu =  Man(); console.log(typeof pangLiu);//undefined console.log(pangLiu.name);//Cannot read property 'name' of undefined   </script>


5, array, 1, var a = [3]; // declare an array with a length of 1 and a [0] = 3.
2, var a = new Array (3); // declare an Array with a length of 3
6. JSON: JSON parsing of key-value contained in braces:
 <script>var jstr = '{"mykey" : "my value"}';// Reverse modevar data = eval('(' + jstr + ')');console.log(data);// Recommendeddata = JSON.parse(jstr);console.log(data.mykey);  </script>

JSON encapsulation:
  <script>var dog = {name : "LaoLIu", dob: new Date(),legs: [1, 2, 3, 4]};var jsonStr = JSON.stringify(dog);console.log(jsonStr);//{"name":"LaoLIu","dob":"2014-11-07T06:17:28.097Z","legs":[1,2,3,4]}  </script>
7. Regular Expression a. Use new RegExp to generate a regular expression. B. Use the literal syntax
8. five basic types of the basic type package Javascript: Numbers, strings, Boolean, null, and undefined.
Corresponding to: Number () String () Boolean ()
<script> var n = 100; console.log(typeof n);//number var nobj = new Number(100); console.log(typeof nobj);//object var s = "hello"; console.log(s.toUpperCase());//HELLO var greet = "1 2 3 4";console.log(greet.split(" ")[0]);//1  </script>
9. Error object (exception capture)
   <script> try{throw{Name: "MyError", // custom error typemessage:'oops',extra: 'This is Exception',Remedy: genericErrorhandler // specify the function to handle this error}; }catch(e){ alert(e.message);E. remedy (); // call genericErrorhandler }function genericErrorhandler(){alert("Hi, BigLiu!");}  </script>























Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.