JavaScript Object Learning Note one

Source: Internet
Author: User

When an object is used as a constructor, it can receive a parameter,

1. If this parameter is a value of the original type, the wrapper object corresponding to the value of the change value is returned

Object (123); // Number     {[[Primitivevalue]]: 123}

2. If this parameter is an object, return the incoming object directly

Object ({  name:' gen '}); //     Object {name: ' Gen '}

You can then determine whether a variable is an object type by using the attribute that receives an object to return the original object.

/*/function  isobject (value)    {return value = = = Object ( Value)}console.log (IsObject ([up]))    //  true//  True 

Two Object.prototype.toString can get a constructor for an instance object, in order to prevent the small partner from customizing the ToString method on the object to overwrite the Object.prototype.toString, we can invoke the function's call method on any value objec T.prototype.tostring method, so we can write a better type judging function than typeof

/** * Pass any parameter return parameter type * @param value * @returns {string} return value is String, same as typeof*/functionGetType (value) {varo =Object.prototype.toString.call (value)returnO.match (/\[object (. *) \]/) [1].tolowercase ()}gettype (1)//"Number"GetType (' null ')//"string"GetType (NULL)//"NULL"GetType ({})//"Object"GetType (function(){})//"function"GetType (NewDate ())//"Date"GetType ()//"undefined"GetType (/GEN/)//"RegExp"
GetType ([])//"array"

Well, with a function that gets the data type, we write a function that is specifically used to determine the type of data that is written before the handwritten code in advance with a literal description

1, first, for example, to determine whether an object is an array? We can use Array.isarray (arr) to judge

var arr = []; Array.isarray (arr);     //     True

2, good, then we GetType method plus Isxxx method

3, isxxx method we can not write one by one, so our xxx method should be an array or a JSON object, can be configured, we here directly define an array bar, array named type

/**/var type    = [' Null ', ' Undefined '    ,' Object ',    ' Array ',    ' String ',    ' number    ',    ' Boolean ', ' Function ',    ' RegExp ',' Infinite '];

4, find a way to achieve similar to: gettype.isxxx (value); GetType is a method, the method itself is an object, there are two ways to access the property of an object (I know two kinds of ha), that is the point (".") ) operators and similar access to array elements of "[]", [] operators have the advantage that the property name is dynamic or not canonical, where our properties are just dynamic plus go, so we use these facilities to build our gettype.isxxx (value)

5, may be said above a bit of chaos, basically on these ideas, the following code implementation

Type.foreach (function  (t) {    /** *     is used to determine a data type     * @param o     * variable     to be judged * @returns {Boolean}     * judgment result, true/false     */    gettype[function (o) {        return getType (o) = = = t.tolowercase ()    })
  true
false
gettype.isundefined () // true
gettype.isundefined (1) // false

JavaScript Object Learning Note one

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.