JavaScript Learning Notes (ii)

Source: Internet
Author: User
Tags tojson

Filter

Filters some elements and returns the remaining elements. example, deleting an even number leaves only odd numbers:

var arr = [1,2,3,4,5,6];     var r = Arr.filter (function(x) {        return x% 2 = 0;    });    Console.log (r);

Closure private variables

The effect of using JavaScript to implement a private variable is as follows:

function Zizeng (x) {        var z = x | | 0;         return {            function() {                + =1                ; return z    ;     }}} var zi = Zizeng ();    Console.log (Zi.add (1));

Generator

In addition to return, you can return multiple uses of yield. Example: Loop output effect

function* addnum (max)    {        var qishi = 0;          while (Qishi < max) {            yield Qishi;            Qishi++        ;        } return Qishi;    }      for (var x of Addnum (5))    {        console.log (x);    }

Note: function*

typeof and Instanceof

TypeOf return Object Type, example: typeof 1; ' Number '

Instanceof is used to determine whether a variable is an instance of an object, for example: "Chenxy" instanceof String; True

New Object and Object

var n = new number (123); The type is actually object, so type try not to add new

var n = number (' 123 '); Convert to number type, similar to Method parseint ()

Numeric conversion string

123.toString (); Exception, should be written as 123..toString () or (123). ToString ()

Regular expressions

\d match numbers,

\w matches letters or numbers,

\s Tab or Space

. Matches any character,

* Any character (including 0),

+ represents at least one character,

? Represents 0 or 1,

{n} represents N,

{N,m} represents a N-m

[] indicates a range, such as: [0-9a-za-z\_] can match a number, letter, or underscore

a| b matches A or B, for example: [J|j]ava can match Java or Java

^ denotes the beginning of the line, for example: ^\d

$ indicates the end of the line, for example: $\d

In JavaScript, there are two ways to define a regular expression

The first type: direct Write, var a =/abc\-001/;

The second type: Instantiate a Regexp,var a = new RegExp (' abc\\-001 ');

Determine if the match

var re =/^\d{3}$/;

Re.test (' 000 '); True

Slicing a string

' A B c d '. Split (/\s+/); [' A ', ' B ', ' C ']

Group

() represents the grouping to extract. After the match succeeds, an array is returned, the first one is itself, followed by a string that matches the success. For example:

var re =/^ (\d{3})-(\d{3,8}) $/;

Re.exec (' 010-123456 '); [' 010-123456 ', ' 010 ', ' 123456 ']

Json

JSON one of six data types. Number, Boolean, string, NULL, array, object.

Dead. The string must be UTF-8 and must be enclosed in double quotes

Serialization of

var xiaoming = {        name:' Morningstar ', age        :        skills:[' Javascript ', ' C # ', ' Oracle ']    };    Console.log (Json.stringify (xiaoming));      // {"name": "Morningstar", "Age": "Skills": ["Javascript", "C #", "Oracle"]}    Console.log (Json.stringify (xiaoming,null, '    )    ); // Formatting    Console.log (json.stringify (xiaoming,[' name ', ' skills '));     // Filter Key value, {"Name": "Morningstar", "Skills": ["Javascript", "C #", "Oracle"]}

You can also pass in a function so that each key value will be processed first by the function

function CONVERT (key, value) {    if (typeof value = = = ' String ') {        return value.touppercase ();    }     return  "  );

You can also customize a Tojson method to return the serialized data directly

var xiaoming = {        name:' Morningstar ', age        :        skills:[' Javascript ', ' C # ', ' Oracle '],        toJSON:function() {            return  {                "Name":  This . Name,                " Age ":the. age};}    ;

Deserialization

Console.log (' {' "name": "Morningstar", "Age": json.parse, "skills": ["Javascript", "C #", "Oracle"]} '));

Object-oriented JS

JS does not go to the concept of classification and instance, all through the prototype (prototype) to achieve object-oriented programming. Example:

For example, I want to buy McDonald's, but we do not have this class. But now there's a KFC class, as follows:

    var KFC = {        name:' KFC ', Money        :$,        Eat:true    };

Except the name is different, the rest is quite similar, use this. As follows:

var KFC = {        name:' KFC ', Money        :$,        eat:function() {            Console.log ("Open eat");        }    ;     function Create (name)    {        var s = object.create (KFC);         = name;         return s;    }     var M = Create ("McDonald");    M.eat ();

M._proto_ = KFC Low version IE does not support this point, try not to use

Prototype chain

Create this function above, the prototype chain is: Create---> Function.prototype---> Object.prototype---> Null

constructor function

function Hello (Name)    {        this. Name = name;           This function () {            alert (' Hello ' + Name);        }    }     var New Hello ("Xiaoming");    Xiaoming.hello ();

Note, be sure to add new

Inherited

Because the two method prototype chain is the same, you need to repair the relationship of the prototype chain. To implement inheritance, the following methods are available:

//repairing the prototype chain    functionInherits (child,parent) {varF =function() {} F.prototype=Parent.prototype; Child.prototype=NewF (); Child.prototype.constructor=Child ; }    functionStudent (props) { This. Name = Props.name | | ' Chenxy '; } Student.prototype.hello=function() {alert (' Hello, ' + This. name+ '! '); }    functionprimarystudent (props) {Student.call ( This, props);  This. grade = Props.grade | | 1; }    //Implementing a prototype chaininherits (Primarystudent,student); //Calling Methods    varPRI =NewPrimarystudent ("Chenxy"); Pri.hello ();

JavaScript Learning Notes (ii)

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.