JavaScript prototypes and prototype chains

Source: Internet
Author: User

Code One:

<! DOCTYPE html>functionCar (desc) { This. desc =desc;  This. color = "Red"; }//Car.prototype = {        //getinfo:function () {        //return ' A ' + this.color + ' + This.desc + '. ';        //            }        //        };        //instantiate object using the constructor functionvar car=new car ("BMW");//var car = object.create (car.prototype);//Console.log (Car.getinfo ());Console.log (carinstanceofCar);        Console.log (Car.color);    Console.log (CAR.DESC); </script>


The above code output results are:

trueRedBMW
<! DOCTYPE html>        var car =  object.create (car.prototype);        Console.log (Car.getinfo ());        Console.log (car instanceof  car);        Console.log (Car.color);        Console.log (CAR.DESC);    </script>

The above code output results:

True

Undefined

Undefined

Here's the problem:

Building an instance with the new+ constructor, the instance can access the properties of the constructor function

Instance objects cannot access the properties of a constructor when building an instance through Object.create (prototype)

The basis of the above results is where. Find out.

JavaScript prototypes and prototype chains

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.