JavaScript first (four)--------object, constructor, wrapper class

Source: Internet
Author: User
Tags wrapper

Object

Everything in JavaScript is an object, such as a string, a number, an array, a date, and so on, the object is the data that owns the property and method, and is the reference value. A property is an object-dependent value that can be

Action to be performed on the object. ( in object-oriented languages, properties and methods are called members of an object)

  There are several ways to create objects:

1. Object literal format, such as

var stu = {                "xiao",                            

2. Constructors

  The system has some self-built constructors, such as New Object ();   Array ();  Number ();  Boolean (); Date ();

var obj = new Object ();

We can manually add some properties and methods to the inside

Obj.name = "Ming"; obj.func = function () {};

3. Custom Constructors

Custom constructors can define their own methods and are suitable for mass production.

function Car (Color,money) {               this.name = "Daben";               This.color = color;               This.money = money;            }           var car1 = new Car ("Reg", "10w");           var car2 = new Car ("Green", "20w");

  Car1 and Car2 are independent of each other, they are constructed with the same custom constructor car, and they have their own unique properties by means of a pass-through argument. (Note that the constructor is to be written with a large hump type.) )

internal mechanism of the constructor function

1.  implicitly Add this = {} at the front of the function (this is an empty object)

2. Execute this.xxx = xxx;

3. Implicitly returns the This object.

Give me a chestnut:

function Person (name,age,sex) {        this. Name = name;          this. Age = Age ;         this. Sex = sex;        } var New Person ("Steven","male"," ");

At the time the person function is defined, first, an empty object named this is implicitly added to the execution context, and then each statement is executed as the property and method of adding the object to this. Finally, all statements are read and executed

, the This object is implicitly returned. If the constructor is not referenced by new, this points to window.

imitating using the internal mechanism of the constructor function
function f (name,age) {        var obj = {};         = name;         = Age ;         return obj;    }
var uu = new F ("JK", 10);

  First we create an empty object named obj, and then we define methods and properties for the Obj object, and finally we explicitly return the Obj object, but this artificial constructor, efficiency, and so on all aspects are not the original good, but also related to the original

The problem of type, so there is a loophole.

Extension of the constructor function

    Change the return value of the above code to see

return 1234;//The object produced is f{}
return true;//             f{}
return "Object";//          f{}
return {};  //              {}

  

  We can find that the first three are the original objects produced by the constructor, but for the fourth one, it becomes an empty object, meaning that if we return if the original value, then the structure has no effect, but if the return

is a reference value, then the result is the return value.

Take a look at an example to digest:

        var a = 5; function test () {a = 0; alert (a);//      0 alert (THIS.A);//  5   undefined var A; alert (a);//      0}        

  

What are the results of the above code running test () and new test ()? Here is the point of this when there is new. The previous one points to the window, so it is 5, the latter point to the function test, which makes the function internal without THIS.A, so it is

Undefined, the other two A is a within the function, so a is 0.

Packing class

  Because the original values are not properties and methods, when we add them to them, the system converts them into the form of the object corresponding to the original value, which we call the wrapper, and the object form of the original value is called the wrapper class.

Change the original value into an object form, for example:

Number ——— The object form of a ———— digit string ———— the new string () ———— the object form of the string Boolean ———— new Boolean () ———— the object form of the Boolean value

  When we add a property to the original value, the JS engine implicitly converts the original value to the corresponding object type, and then the delete operation means that when we add a property to the original value, the engine will immediately delete the original value's object form.

For example:

var num = 123; NUM.ABC = "a"; Num.abc = undefined

 

and a chestnut to digest and digest.

var str = "abc"; str + = 1; var test = typeof (str); if (test.length = = 6) {test.sign = "typeof return result may be string";} Doucment.write (test.sign);

  

 See what the final output is? A rough look at Test is a string, just good length is 6, print "typeof return result may be a string", really, the result is printed undefined,why!!! Originally string is the original

Value, when we give him the attribute sign, JS engine deleted its object form, so it is undefined, here to pay attention to the packing class problem.

JavaScript first (four)--------object, constructor, wrapper class

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.