Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Commonmark migration
Source Link

All JavaScript objects inherit properties and methods from a prototype.

The JavaScript prototype property also allows you to add new methods objects constructors:

ie : Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Person objects inherit from Person.prototype.

<script>
 var Invoice = function(subtotal, taxRate) {
 this.subtotal = subtotal;
 this.taxRate = taxRate;
};
Invoice.prototype.getTaxAmount = function() {
 return (subtotal * this.taxRate);
};
var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>

All JavaScript objects inherit properties and methods from a prototype.

The JavaScript prototype property also allows you to add new methods objects constructors:

ie : Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Person objects inherit from Person.prototype.

<script>
 var Invoice = function(subtotal, taxRate) {
 this.subtotal = subtotal;
 this.taxRate = taxRate;
};
Invoice.prototype.getTaxAmount = function() {
 return (subtotal * this.taxRate);
};
var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>

All JavaScript objects inherit properties and methods from a prototype.

The JavaScript prototype property also allows you to add new methods objects constructors:

ie : Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Person objects inherit from Person.prototype.

<script>
 var Invoice = function(subtotal, taxRate) {
 this.subtotal = subtotal;
 this.taxRate = taxRate;
};
Invoice.prototype.getTaxAmount = function() {
 return (subtotal * this.taxRate);
};
var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>
Source Link
Saurin Vala
  • 1.9k
  • 1
  • 18
  • 25

All JavaScript objects inherit properties and methods from a prototype.

The JavaScript prototype property also allows you to add new methods objects constructors:

ie : Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Person objects inherit from Person.prototype.

<script>
 var Invoice = function(subtotal, taxRate) {
 this.subtotal = subtotal;
 this.taxRate = taxRate;
};
Invoice.prototype.getTaxAmount = function() {
 return (subtotal * this.taxRate);
};
var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>
lang-js

AltStyle によって変換されたページ (->オリジナル) /