README.md (2234B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Prototypical Inheritance 22 23 > Implement prototypical inheritance by replacing the prototype of one constructor with the prototype of another constructor. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var inherit = require( '@stdlib/utils/inherit' ); 37 ``` 38 39 #### inherit( ctor, superCtor ) 40 41 Implements prototypical inheritance by replacing the prototype of one constructor with the prototype of another constructor. 42 43 <!-- eslint-disable no-restricted-syntax --> 44 45 ```javascript 46 function Foo() { 47 return this; 48 } 49 50 Foo.prototype.beep = function beep() { 51 return 'boop'; 52 }; 53 54 function Bar() { 55 Foo.call( this ); 56 return this; 57 } 58 59 inherit( Bar, Foo ); 60 ``` 61 62 </section> 63 64 <!-- /.usage --> 65 66 <section class="notes"> 67 68 ## Notes 69 70 - This function is not designed to work with ES2015/ES6 classes. For ES2015/ES6 classes, use `class` with `extends`. 71 72 </section> 73 74 <!-- /.notes --> 75 76 <section class="examples"> 77 78 ## Examples 79 80 <!-- eslint-disable no-restricted-syntax --> 81 82 <!-- eslint no-undef: "error" --> 83 84 ```javascript 85 var inherit = require( '@stdlib/utils/inherit' ); 86 87 function Foo() { 88 return this; 89 } 90 Foo.prototype = {}; 91 Foo.prototype.beep = function beep() { 92 return 'boop'; 93 }; 94 95 function Bar() { 96 Foo.call( this ); 97 this._super = Foo.prototype; 98 return this; 99 } 100 101 // Set up prototypical inheritance: 102 inherit( Bar, Foo ); 103 104 var bar = new Bar(); 105 106 var bool = ( bar instanceof Bar ); 107 // returns true 108 109 bool = ( bar instanceof Foo ); 110 // returns true 111 112 bool = bar.beep(); 113 // returns 'boop' 114 ``` 115 116 </section> 117 118 <!-- /.examples --> 119 120 <section class="links"> 121 122 </section> 123 124 <!-- /.links -->