Some questions about JS's clone () function __ function

Source: Internet
Author: User
Tags hasownproperty

Problem: Using JS to implement a clone () Cloning function, the function will input into the different types of values Number,string,undefined,boolean,function,null,object,array,regexp, Cloning a copy out of a, problem-solving code

Directly attached code,

function Clone (obj) {            var copy;             switch ( typeof obj) {                case ' undefined ': break;         & nbsp       case ' number:                 case ' string ':     & nbsp           case ' Boolean ':                 case ' function '
: copy = Obj;break;                 Case ' object ':               &N Bsp
    if (obj = null) copy = NULL;                     ELSE if (tostring.call (obj) = = ' [object Array] ') &NB Sp                                   & nbsp
      copy = [];       &NBSP
                for (var i in obj) Copy.push (Clone (Obj[i));                                   &     nbsp
else if (tostring.call (obj) = = ' [object RegExp] ')                     {
                        copy = obj;                                   & nbsp     else                      {      &NBS P
                copy = {};                          for (var j in obj)      
                      copy[j]= clone (Obj[j]); &nbSp                    {           }    
        return copy;
       }         var a=undefined;
        var b=1;
        var c= "Hello";
        var d=true;
        var add=function (a,b) {        return a+b;        }
        var e=null;
        var f=[1,2,3];
        var g=/^\s+/;         var h={        a:1,         b:2     &NBSP ;
 }         Console.log (typeof clone (a));
        Console.log (typeof clone (b));
        Console.log (typeof clone (c));
        Console.log (typeof clone (d));       &NBSP
Console.log (Clone (add) (1,2));
        Console.log (Object.prototype.toString.call (Clone (e)));
        Console.log (Object.prototype.toString.call (Clone (f)));
        Console.log (Object.prototype.toString.call (Clone (g)));          Console.log (Object.prototype.toString.call (Clone (h)));

Results:


second, the question

When I first saw this problem, I thought TypeOf [1,2,3] The result is, this can do, regular expression, Null,object typeof are all. Look at the code above, which is solved with Object.prototype.toString.call (obj) or tostring.call (obj).

So why not just use obj.tostring (). Let's take a look at what obj.tostring () will output.


Null and undefined actually went wrong, that's for sure, because ToString () is not capable of completing null and undefined transformations, using string () to


If string () is not converted to null or undefined, it is automatically converted to ToString (). It's far away. Let's get back to business.

So what's the result of using Object.prototype.toString.call (obj)?


Incredibly different, what's going on here.

It turns out that although types such as Array,null are instances of object, but each of them rewrites the ToString () method, we try to verify that:

var arr=[1,2,3];
Console.log (Array.prototype.hasOwnProperty ("tostring"))//Determine whether the prototype has a toString () method
Console.log (arr.tostring ()) ;
Delete array.prototype.tostring;//deletes the rewritten toString
console.log ("Array.prototype.hasOwnProperty" in the Array prototype) ToString "));
Console.log (Arr.tostring ());

Results:


It's obvious that it's been rewritten. third, there are some words

In fact, some people will say that you can use arr instanceof array to determine whether the array, in fact, instanceof in the frame object built in the case will be invalidated.



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.