<html>
<head>
</head>
<body>
<script type="text/javascript">
// Calculator1 "class" declaration
function Calculator1(){
/* instance multiply sum that will be available for the
Calculator1 objects but not for the Calculator1 "class" */
this.multiply = function(val1 , val2){
return (val1*val2);
}
}
/* instance method sum that will be available for the
Calculator1 objects but not for the Calculator1 "class" */
Calculator1.prototype.sum = function(val1 , val2){
return (val1+val2);
}
// calculator1 object/instance
var calc1 = new Calculator1();
// call to multiply, that is an instance method
console.log(calc1.multiply(4,3)); // 12
/* error because multiply is an instance
method not a Calculator1 "class" method */
//console.log(Calculator1.multiply(4,3));
// call to sum, that is an instance method
console.log(calc1.sum(4,3)); // 7
/* error because sum is an instance method
not a Calculator1 "class" method */
//console.log(Calculator1.sum(4,3));
// Calculator2 "class" declaration
function Calculator2(){}
// static method of the "class" Calculator 2
Calculator2.multiply = function(val1 , val2){
return (val1*val2);
}
var calc2 = new Calculator2();
// call to multiply, that is a Calculator2 "class" method
console.log(Calculator2.multiply(4,3)); // 12
/* error because multiply is a Calculator2 "class" method
not an instance method and it doesn't get inherited to be called
from instances, it can only be called from the Calculator2 "class" */
//console.log(calc2.multiply(4,3));
</script>
</body>
</html>
Resources:
No comments:
Post a Comment