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