Not all JavaScript objects are objects.

Source: Internet
Author: User

The values in the original value vs object javascript can be divided into two categories: the original value (primitive) and the object (object ). Define the two types of javascript values: the following values are the original values. String number: All numbers in JavaScript are floating-point Boolean values. nullundefined all other values are objects ). Objects can be further divided: the wrapper of the original value: Boolean, Number, String. Rarely used directly. Objects created using the literal. You can create an object by using the constructor. You can use the literal to create an object. [] Is new Array () {} is new Object () function () {} is new Function ()/\ s */is new RegExp ("\ s *") date: new Date ("2011-12-24") You can use the enumerated primitive and the Defined Object non-primitive to define the primitive and object. But you can also describe the primitive and object. Let's start with the object. The object is variable:> var obj ={};> obj. foo = 123; // Add attributes and values 123> obj. foo // read attribute. The returned attribute value is 123. Each object has its own unique identifier. Therefore, the object created by a literal or constructor is not equal to any other object, we can use = for comparison.> {}=={} False objects are compared by reference. Only two objects with the same identifier are considered equal.> Var obj ={};> obj = obj true variable saves the object reference. Therefore, if the two variables apply the same object -- when we change one of the variables, the two will also change.> Var var1 ={};> var var2 = var1;> var1.foo = 123; // modify the attribute 123 of the variable val1> var2.foo // val2 also changed 123 as expected, the original value is different from the object: The original value is unchangeable; you cannot add attributes to them:> var str = "abc";> str. foo = 123; // Add attributes (this operation will be ignored) 123> str. foo // read attribute value. The returned undefined original value does not have an internal identifier. The original value is compared by value: the basis for comparing two original values is their content, if the content of the two original values is the same, the two original values are considered to be the same.> "Abc" = "abc" true means that an original value is its value, and the javascript engine does not assign a unique identifier to the original value. The combination of the last two facts means that we cannot distinguish whether a variable is an object reference or a copy of the original value. Trap: original values and their packaging type rules: Ignore as many packaging types as possible. In other programming languages such as Java, you seldom notice them. The original value types 'boolean', 'number', and 'string' have their own packaging types 'boolean', 'number', and 'string. An instance of the packaging type is an object value, and the conversion between the two types is also very simple: convert to the packaging type: new String ("abc") to the original type: new String ("abc "). valueOf () primitive value types and their corresponding package types have many differences, such as:> typeof "abc" 'string'> typeof new string ("abc ") 'object'> "abc" instanceof Stringfalse> new String ("abc") instanceof Stringtrue> "abc" = new String ("abc ") an instance of the false packaging type is an object. Therefore, like JavaScript and objects, the packaging type cannot be compared with values (only reference can be compared ).> Var a = new String ("abc");> var B = new String ("abc");> a = bfalse // although a and B have the same content, but still return false> a = atrue original value does not have its own method to wrap object type is rarely used directly, however, their prototype objects define many methods that can be called with their corresponding original values. For example, String. prototype is the prototype of the String packaging type. All its methods can be used on the original string value. Method of packaging type String. prototype. indexOf also has the original string values. They are not two methods with the same name, but are indeed the same method:> "abc ". charAt = String. prototype. charAttrue has the toFixed method in the prototype of the Number packaging type, that is, Number. prototype. toFixed, but an error occurs when we write the following code:> 5. toFixed (3) SyntaxError: Unexpected token ILLEGAL this error is a parsing error (SyntaxError), followed by a dot (.) After 5 (.), this point is treated as a decimal point, and it should be followed by a number. The following code can run normally:> (5 ). toFixed (3) "5.000"> 5 .. toFixed (3) "5.000" value Classification: typeof and instanceof For classification, you need to pay attention to the difference between the original value and the object. The typeof operation can be used to distinguish between the original value and the object. Instanceof can be used to differentiate objects. In addition, instanceof returns false for all original values. Typeoftypeof can be used to determine the type of the original value and distinguish between the object Value and the original value:> typeof "abc" 'string'> typeof 123 'number'> typeof {} 'object'> typeof [] 'object' typeof returns the following string: parameter result undefined "undefined" null "object" boolean value "boolean" number "number" string "string" function "function" other "object" Note: typeof returns "object" when the operation is null, which is a bug in the JavaScript language. Unfortunately, this bug will never be fixed, because too many existing code already depends on this performance. This does not mean that null is actually an object [4]. Typeof also allows you to check whether a variable has been declared without throwing an exception. No function can implement this function, because you cannot pass an undeclared variable to a function parameter.> Typeof undeclaredVariable 'undefined'> undeclaredVariable ReferenceError: The undeclaredVariable is not defined function is also an object type, which may not be understood by many people, but is sometimes very useful. An array is an object.

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.