Some knowledge fragments of JavaScript (1)

Source: Internet
Author: User

Some knowledge fragments of JavaScript (1)
I plan to raise the Javascript usage level from child to elementary school, and will keep some knowledge points in the near future. Javascript reference mechanism: a reference is established as long as an object is assigned a value to another object. Once a reference is created, objects share a space. You can refer to the following code: copy the code var stooge = {nickname: "Maria"} var x = stooge; console. log (stooge. nickname); // Maria x. nickname = 'curly '; x. realname = "Thomas" console. log (stooge. realname); // The realname attribute added by Thomas by x. Var nick = stooge. nickname; console. log (nick); // apparently the Curly console. log (stooge. nickname); // It is overwritten. There is no strong class concept in Curly copy code java, and there is no distinction between class and instance. The usage of the new Keyword is similar to that of java. It is also used to create a new object. However, objects in js cannot be created only by using the new method. For example, objects of the function type can be created by explicitly declaring, assigning values to variables, or calling the Function constructor. Javascript Prototype: each object has a prototype and inherits attributes from it. All objects inherit the attributes of Object. prototype. When creating a new object, you can select an object as prototype to directly inherit its attributes. Here is a new memory space, rather than reference. Through the following code, we can easily let an Object inherit the prototype of another Object to copy the Code if (typeof Object. inherit! = 'Function') {Object. inherit = function (obj) {var fun = function () {}; fun. prototype = obj; return new fun () ;};} var lucasliu = {job: "tester", holobby: "reading"} var a = Object. inherit (lucasliu); // "inherit", in fact, all properties are copied to the console. log (. job); // tester inherits from lucasliua. job = 'Doctor'; console. log (. job); // The doctor value is overwritten as the doctorconsole. log (lucasliu. job); // tester, lucasliu is not affected. newskill = "play piano"; console. log (A. newskill); // play piano a and prototype get new skills.

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.