20180423-20180428 Front-end knowledge filling hole

Basic syntax of Class

In the JavaScript language, the traditional way to generate instance objects is through constructors. Below is an example.

function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};

var p = new Point(1, 2);

The above way of writing is very different from traditional object-oriented languages ​​(such as C++ and Java), and it is easy to confuse programmers new to the language.

ES6 provides a way of writing that is closer to traditional languages, and introduces the concept of Class (class) as a template for objects. With the class keyword, classes can be defined.

Basically, ES6 class can be regarded as just a syntactic sugar, and most of its functions can be done by ES5. The new class writing method just makes the writing method of object prototype clearer and more like the syntax of object-oriented programming. The above code is rewritten with ES6 class, which is as follows.

// Define class 
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}

The above code defines a "class", you can see that there is a constructor method in it, which is the constructor method, and the this keyword represents the instance object. That is to say, the constructor Point of ES5 corresponds to the constructor of the Point class of ES6.

In addition to the constructor, the Point class also defines a toString method. Note that when defining the method of "class", you don't need to add the keyword function in front of it, you can directly put the function definition in it. In addition, there is no need to separate the methods with commas, and an error will be reported if added.

————————————————————————————— Reprinted from http://es6.ruanyifeng.com/#docs/class For more information, please go to ~

 

Difference between URI and URL

First of all, let me give you an example. One day, the general manager of a company gave me a business card with his title on it, Zhang San, general manager of Beijing XXX Company, and his office address, Haidian District, Beijing The office of the general manager of Beijing XXX Company, No. 35, Chang'an Street, then, I will brag to my friends in the future. I know Zhang San, the general manager of Beijing XXX Company. My friends all know that the general manager of Beijing XXX Company is a person named Zhang San. , then, this title corresponds to Zhang San. As long as this title is mentioned, everyone knows that it is Zhang San. In the online world, this title is called URI. As long as you give me a URI, I will know What does it represent? For example, http://www.sina.com.cn represents Sina.com, [email protected] represents a person's QQ mailbox, and your QQ number is also a URI (identifiable in Tencent's server). It is your QQ account), URI is the title of network resources, through the URI mark can mark and distinguish everything in the network world.

OK, now there is a problem, you now know that the general manager of Beijing XXX Company is Zhang San, and "General Manager of Beijing XXX Company" is the URI of Zhang San, but I asked you to meet Zhang San in person, you Can it be done? You definitely can't, because you don't know his address, although you have his URI title, but other than that, you don't know anything about his specific situation, so if you want to locate him, you have to get him You can find Zhang San through the address of "General Manager Office of Beijing XXX Company, No. 35 Chang'an Street, Haidian District, Beijing". Reflecting on the network world, each resource in the network world not only has its own title, but also can be accessed and found by people. Therefore, the network address is necessary. Otherwise, the existence of this network resource has no meaning. This address It's called a URL.

From the above description, it can be found that URI emphasizes naming resource tags, and URL emphasizes locating resources, but you will find that URL contains more information than URI. I can also know from the URL that Zhang San is the general manager , and I also know his address, so in most cases, people think that it is too troublesome to name and address a network resource separately, so simply use the address as both an address and a tagname, so the URL also acts as a The role of URI in the WWW world wide web, but it has more meaning than URI, I not only know what your name is, I also know where you are. All we enter in the browser are URLs, because the purpose of our input is to find a certain resource. Of course, it is also correct that you enter a URI, because a URL is also a URI.

To summarize: URI marks a web resource, nothing more; URL marks a WWW Internet resource (marked with an address) and gives its access address.

——Solution ideas——————-
url is an address, uri is a resource.
You'd better understand rest and odata, so that you can have a deeper understanding of uri.

——————————————————————————————————————————

The difference between escape, encodeURI, encodeURIComponent

Both escape and encodeURI belong to Percent-encoding. The basic function is to convert illegal characters of URI into legal characters. The converted form is similar to "%*". The fundamental difference between them is that when escape processes characters other than 0xff, it directly uses the unicode of the character and adds a "%u" in front, while encodeURI is first UTF-8, and then in each UTF-8 Add a "%" before the bytecode; when processing characters within 0xff, the encoding method is the same (all are "%XX", XX is the hexadecimal unicode of the character, and it is also the UTF-8 of the character), but The ranges (i.e. which characters encode which characters don't) are not the same. (Yang Yi reminds) encodeURI is a standard of W3C, while Escape is a non-standard. Take an example: Zhihu's "zhi" character, you can know that its unicode is 0x77e5, encode it in UTF-8, and it becomes three bytes: 0xe7, 0x9f, 0xa5 Therefore, if you use escape encoding "zhi" ", The result obtained is "%u77E5"; the result obtained by encodeURI is "%E7%9F%A5". The encoding of Escape has a disadvantage that it is followed by 4-digit hexadecimal, so it does not support the basic multilingual plane (BMP). ) characters (unicode greater than 0xffff); and encodeURI is based on UTF-8, the encoding itself can theoretically support characters within 0x10ffff (in fact, current JavaScript does not support characters outside BMP, so encodeURI does not support it either) . The difference between encodeURI and encodeURIComponent is that the range of characters that need to be escaped is different, please refer to @黄家兴's answer. In terms of usage scenarios, you can also refer to his answer, but I don't agree with this sentence: if it is just an encoded string and has no relationship with the URL, then use escape. encodeURI(Component) is generally used on URIs, but not necessarily on URLs. For example, if the Content-Type in the Request Header of the POST request is "application/x-www-form-urlencoded", the data in the Request Payload is generally encoded using encodeURI(Component). (same as URL querystring). Do not use escape if it is not necessary.

————————————————————————————————————————————— Transfer from self-knowledge https:// www.zhihu.com/question/21861899

————————————————————————————————————————————————————————

The Object.create() method will use the specified prototype object and its properties to create a new object.

————————————

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028538&siteId=291194637