Nodejs Grammar is a very special place.

Source: Internet
Author: User
Tags knowledge base

1. But we've said it before. There is a difference in scope between this and no this, and that parameter only works in the constructor, and the one that adds this is the member variable. With a This it is immediately distinguishable from them, so even if the same name does not matter.

2.Declaration and instantiation of a class

Declaring a class is very simple, let's not laugh:

Javascript
function foo () {
//...
}
Well, we've already written a Foo class.

It's a fake?! It's true.

Don't believe me? I don't believe you can go on and play a code to see:

Javascript
var bar = new Foo ();
Although it is a function, if it is written in this form (new), it is the instantiation of this class.

And this so-called Foo () is actually the constructor of this foo () class.

3. Methods for writing member variables:

The first method:

Copy CodeThe code is as follows:
Javascript
function foo () {
This.hello = "World";
}

Note: The member variable of the class is called only when this is added, otherwise it is just a local variable within the function. The scope of the variable is to be clearly divided.

The second method is declared outside the constructor or any member function, whose format is < class name >.prototype.< variable name;:

4. Pass parameters can be more or less than the declaration.

We started by saying that JavaScript is a weak type language, in fact, not only the weak type, its parameters are very not rigorous. You can pass or pass less (as long as you make sure you pass or pass less, you can ensure that the program is not wrong, or the logic is not wrong), in principle is possible. Multi-pass parameters are automatically ignored, and the less-transmitted parameters are undefined.

5. 

This is my nonsense again. In the so-called randomness of JavaScript, you can modify your class anywhere, and there is some similarity to Ruby.

String, for example, is actually a class, with member variables such as length, as well as member functions such as IndexOf, substr, and so on. But in case we feel that this string is imperfect and wants to add its own method, you can add a function to it where you want it, such as:

Javascript

Copy CodeThe code is as follows:
STRING.PROTOTYPE.SB = function () {
var newstr = "";
for (var i = 0; i < this.length; i++) {
if (i% 2 = = = 0) Newstr + = "s";
else newstr + = "B";
}
return newstr;
};

6.  deep copy

The so-called deep copy is to create a new array or object, the source array or the object of the underlying type variable values manually copied over, rather than just the source array or object reference to take over. So this involves a recursive invocation of something.

Here is a deep copy function I implemented, you can write your own and then add to your own node. JS Knowledge Base.

Javascript

Copy CodeThe code is as follows:
function Cloneobject (SRC) {
var dest = {};
for (var key in Src) {
if (typeof src = = = = "Object") Dest[key] = Cloneobject (Src[key]);
else Dest[key] = Src[key];
}
return dest;
}

Nodejs Grammar is a very special place.

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.