JavaScript Object-oriented support (bottom)

Source: Internet
Author: User

Eight, JavaScript object-oriented support

4. Instance and instance references

The. NET Framework Convention "Everything is Object" to the CTS (Common type System), and is divided into "value type" and "reference type". Where the value type object is converted to reference type data, a "boxing" and "unboxing" procedure is required. There are similar problems in JavaScript. We see the typeof keyword, which returns the following six types of data:

' Number ', ' String ', ' Boolean ', ' object ', ' function ' and ' undefined '. We also found that in the object system of JavaScript, there are four kinds of object constructors, string, number, Function, Boolean. So, our question is: if there is a result of a number a,typeof (a), will it be ' numbers ' or the object that a constructor points to function number ()?

Test code for types of JavaScript

function getTypeInfo(V) {
return (typeof V == 'object' ? 'Object, construct by '+V.constructor
: 'Value, type of '+typeof V);
}var A1 = 100;
var A2 = new Number(100);
document.writeln('A1 is ', getTypeInfo(A1), '<BR>');
document.writeln('A2 is ', getTypeInfo(A2), '<BR>');
document.writeln([A1.constructor === A2.constructor, A2.constructor === Number]);

The results of the test code are as follows:

A1 is Value, type of number
A2 is Object, construct by function Number() { [native code] }
true,true

We notice that both the A1 and the A2 constructors point to number. This means that the object is recognized by the constructor attribute, and (sometimes) is more efficient than the TypeOf. Because the value type data A1 is treated as an object, it has exactly the same characteristics as the A2.

--In addition to the problems associated with instance references.

Referring to the JScript manual, we do the same thing with other base types and constructors to discover:

-Undefined, number, Boolean, and string in the underlying type, the value type variable

-Array, function, and object in the underlying type, which is the reference type variable

-Use the new () method to construct an object, which is the following code for the reference type variable to illustrate the difference between a value type and a reference type:

About value/reference issues in the JavaScript type system

var str1 = 'abcdefgh', str2 = 'abcdefgh';
var obj1 = new String('abcdefgh'), obj2 = new String('abcdefgh');document.writeln([str1==str2, str1===str2], '<br>');
document.writeln([obj1==obj2, obj1===obj2]);

The results of the test code are as follows:

-----------
true, true
false, false
-----------

We see that both the equivalence operation (= =) and the congruent operation (= = =) have different understandings of "object" and "value".

Further understanding of this phenomenon, we know:

-the result of the operation is a value type, or when the variable is a value type, equivalent (or congruent) comparisons can get the desired result-(even if it contains the same data,) different object instances are not equal (or congruent)-between different references to the same object, equal (= =) and congruent (= = =), but for string types, One thing to add: According to JScript, two string comparisons, as long as one is a value type, are compared by value. This means that in the above example, the code "STR1==OBJ1" will get the result true. and the congruent (= = =) operation needs to detect the consistency of the variable type, so "str1 Returns false for the result of ===obj1.

Function arguments in JavaScript always pass in the value parameter, and the reference type (the instance) is passed in as a pointer value. So the function can rewrite the entry variable at will, without having to worry about the external variable being modified. However, you need to be aware of variables that pass in the reference type, Because of its method invocation and property read and write, it may affect the instance itself.-However, you can also pass data by referring to a parameter of the type.

Finally, the value type comparison detects the data in an object instance byte by bit. Low efficiency but high accuracy while reference types detect only instance pointers and data types, they are highly efficient and less accurate. If you need to detect whether two reference types really contain the same data, you might want to try converting it to "string value" To compare.

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.