js can define its own class
<script type="text/javascript"> var Anim = function() { alert('nihao'); }; Anim.prototype.start = function() { alert('start'); }; Anim.prototype.stop = function() { alert('stop'); }; var myAnim = new Anim(); myAnim.start(); myAnim.stop(); </script>
Anim is a class, and nihao will pop up during initialization.
It has two methods, one is the start method and the other is the stop method.
When using it, use 'dot' to call it directly.
<script type="text/javascript"> var Anim = function() { alert('nihao'); }; Anim.prototype = { start: function() { alert('start'); }, stop: function() { alert('stop'); } }; var myAnim = new Anim(); myAnim.start(); myAnim.stop(); </script>
Another definition method, the same effect as above.
<script type="text/javascript"> var Anim = function() { alert('nihao'); }; Function.prototype.method = function(name, fn) { // 这个很有作用 this.prototype[name] = fn; }; Anim.method('start', function() { alert('start'); }); Anim.method('stop', function() { alert('stop'); }); var myAnim = new Anim(); myAnim.start(); myAnim.stop(); </script>
The above is the detailed content of Detailed explanation of javascript class definitions, attributes, and method calling techniques with examples. For more information, please follow other related articles on the PHP Chinese website!