JS Basics How to understand objects

Source: Internet
Author: User

Chatting with several colleagues these days has found that they don't know much about JavaScript when they should use new.

1. When should the new JavaScript function be new?
I think the main problem is to focus on the weak type of JavaScript.


What's new?

First we know what new is doing, and the language experience with our Java or. NET is clearly in the creation of objects. Yes, whether it's Java or. Net. He is creating an object.

What's behind new?

So let's consider what's behind new, (Java and. NET) are typically class names that are decorated with a class. So let's consider the example of what an object is or what the designer's purpose is, so in general the object will contain these members, attributes and behaviors, or one of them (we have only the DAO behavior in Java EE, pojo only attributes), Have you ever seen an object that has neither attributes nor behavior (of course, the class can be so designed, but meaningless).

What's new with JavaScript?
So what does JavaScript new do? New actually opens up a memory space to create an object this object is this, and then this prototype points to the prototype of the function itself.
That is, to new a function, open up a This, the this is the object itself, and then think you go to instance an object. You want to get a member of an object, a property or method, or one of them. This itself has several ways to declare a member, a way of declaring it in the function body, for example: THIS.A = ' 123 ', which is actually assigning a ' 123 ' string to his A property, and of course, it can also assign a value behavior. There is also a way to inherit through the prototype, through the function of the. prototype way.
Well, because JavaScript is a weak type, he doesn't have a class modifier, doesn't have a return value modifier, and has only one function modifier. So it's hard to know if a function is a constructor. So if you need to go to new, you need to figure out the purpose of function, whether he is a constructor or not.

Instance Code description
Since he is a weak type, they can choose whether to use the constructor function or the ordinary function according to the internal conditions. In addition, many of our programmers are from other languages, and few people learn JavaScript systematically, so it's confusing.
Let's look at examples of confusion.

JS Code

    • function Hello () {  
    •     THIS.A = ' 123 ';   
    • &N Bsp   this.b = function () {  
    •         alert (' B ');   
    • li>   }  
    •   
    •     return this;  
    • } & nbsp;
    •   
    • hello.prototype.c = function () {  
    •     alert (' C ');   
    • }   
    • var ahello = new Hello (),   
    • var Bhello = Hello ();   
    • alert (Ahello. a)   
    • alert (BHELLO.A)   
    • ahello.b ();   
    • bhello.b ();   
    • ahello.c ();   
    • bhello.c ();   

You will find that bhello.c () is unable to perform. Look at the function above, why he can be new and not new.
New actually opens up a memory space to create an object this object is this, and then this prototype points to the prototype of the function itself.
No new is executing this function, and the last return this is the host that returns the execution function, which is actually the window itself.
So the C function is bound on the prototype of Hello, so there is no at all on window.

However, there is a problem, that is, if the C method is not executed or C itself is not inherited by the prototype way (that is, not through the prototype way of Hello, by directly assigning a value to this way), actually created the content of this and window added is the same, Programmers will instinctively assume that new and not new are the same. So it fell into the pit.

Summary: JavaScript weakly typed language, a function even if it is a normal execution function, you new or not new will appear compiler exception.
New and not new we have to do the following points.
1, first of all to understand what new is doing, and then look at the intent of the creator of the function, see function structure.
2. When a good programmer provides a JavaScript tool class or a common method, it should comment on how to tell your callers how to use the common content you provide, or provide a factory method.

JS Basics How to understand objects

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.