JS prototypical inheritance test

Developer
Size
2,436 Kb
Views
12,144

How do I make an js prototypical inheritance test?

What is a js prototypical inheritance test? How do you make a js prototypical inheritance test? This script and codes were developed by Jeremias Erbs on 08 December 2022, Thursday.

JS prototypical inheritance test Previews

JS prototypical inheritance test - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>JS prototypical inheritance test</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ body { margin: 2em;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <pre class="logger"> *** Start logging ***<br></pre> <script src="js/index.js"></script>
</body>
</html>

JS prototypical inheritance test - Script Codes CSS Codes

body { margin: 2em;
}

JS prototypical inheritance test - Script Codes JS Codes

// A constructor is just a function that is called with the 'new' keyword.
// With doing so, you can refer to the created instance via 'this'.
// To add a method to all instances, add it to the constructor's prototype.
// create first constructor
var Mammal = function () { this.init = function (name) { this.name = name; log("Mammal name: " + this.name); } this.sound = "Graaawgh";
};
// add a talk method to first constructor
Mammal.prototype.talk = function() { log("Mammal says " + this.sound);
}
// create second constructor
var Cat = function() { this.sound = "Meow"; this.talk = function() { log("Cat says " + this.sound); }
};
Cat.prototype = new Mammal(); // make it a Mammal
Cat.prototype.constructor = Cat; // keep the constructor
// create instance of Cat
var kitty = new Cat;
kitty.init("Kitty"); // Mammal name: Kitty
kitty.talk(); // Cat says Meow
// create instance of Mammal
var someMammal = new Mammal;
someMammal.init("Olli"); // Mammal name: Olli
someMammal.talk(); // Mammal says Graaawgh
log("<br> *** Change Olli's sound: ***");
someMammal.sound = "Woohoo";
someMammal.talk(); // Mammal says Woohoo
kitty.talk(); // Mammal says Woohoo
log("<br> *** Check instanceof: ***");
log("Kitty is Mammal", kitty instanceof Mammal); // true
log("Kitty is Cat", kitty instanceof Cat); // true
log("Olli is Mammal", someMammal instanceof Mammal); // true
log("Olli is Cat", someMammal instanceof Cat); // false
log("<br> ***check constructor: ***")
log("Kitty is Mammal", kitty.constructor === Mammal); // false
log("Kitty is Cat", kitty.constructor === Cat); // true
log("Olli is Mammal", someMammal.constructor === Mammal); // true
log("Olli is Cat", someMammal.constructor === Cat); // false
//////////////////////////////////////////////
// create a logger to show things on screen //
//////////////////////////////////////////////
function log (content) { var output = document.getElementsByClassName("logger")[0], content = Array.prototype.slice.call(arguments).join(": "); output.innerHTML += content + "<br>";
};
JS prototypical inheritance test - Script Codes
JS prototypical inheritance test - Script Codes
Home Page Home
Developer Jeremias Erbs
Username badabam
Uploaded December 08, 2022
Rating 3
Size 2,436 Kb
Views 12,144
Do you need developer help for JS prototypical inheritance test?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Jeremias Erbs (badabam) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!