Download pool/shard003/JavaScript/0019035_03a-constructor.js from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 913 Bytes
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard003/JavaScript/0019035_03a-constructor.js
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard003/JavaScript/0019035_03a-constructor.js
-
curl -L -o 0019035_03a-constructor.js https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard003/JavaScript/0019035_03a-constructor.js
913 Bytes
| //You can create an object with these two steps: | |
| //- Define the object type by writing a constructor function. There is a strong convention, with good reason, to use a capital initial letter. | |
| //- Create an instance of the object with new. | |
| //This is a constructor function | |
| function Car() {} | |
| var car1 = new Car(); | |
| console.log(car1.color); // undefined | |
| //You use "prototype" to add new properties to "class" | |
| //If JS does not find it in object, | |
| //it searches up in prototype hierarchy | |
| Car.prototype.color = null; // default value | |
| Car.prototype.changeColor = function (newColor) | |
| { | |
| Car.prototype.color= newColor; | |
| } | |
| console.log(car1.color); // null, found default | |
| car1.color = "black"; | |
| console.log(car1.color); // black | |
| // https://blog.pivotal.io/labs/labs/javascript-constructors-prototypes-and-the-new-keyword | |
| console.log(car1 instanceof Car); | |
| //true | |
| console.log(car1.constructor); | |
| //[Function: Car] |