Skip to content

Commit a2de085

Browse files
committed
Add simplified version of mixins
1 parent 15bb7c6 commit a2de085

File tree

1 file changed

+61
-44
lines changed

1 file changed

+61
-44
lines changed

README.md

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6539,40 +6539,57 @@
65396539
65406540
335. ### Does javascript uses mixins
65416541
6542-
Mixin is a generic object-oriented programming term - is a class containing methods that can be used by other classes without a need to inherit from it. In JavaScript we can only inherit from a single object. ie. There can be only one `[[prototype]]` for an object.
6542+
JavaScript does not have built-in support for mixins as a formal language feature. However, developers commonly implement mixins using various patterns to enable code reuse and composition.
65436543
6544-
But sometimes we require to extend more than one, to overcome this we can use Mixin which helps to copy methods to the prototype of another class.
6545-
6546-
Say for instance, we've two classes `User` and `CleanRoom`. Suppose we need to add `CleanRoom` functionality to `User`, so that user can clean the room at demand. Here's where concept called mixins comes into picture.
6544+
A mixin is a way to add reusable functionality from one or more objects into a class or another object, without using classical inheritance. It promotes object composition by combining behaviors or properties from different sources into a single destination.
65476545
6546+
### Mixin Example using Object composition
65486547
```javascript
6549-
// mixin
6550-
let cleanRoomMixin = {
6551-
cleanRoom() {
6552-
alert(`Hello ${this.name}, your room is clean now`);
6553-
},
6554-
sayBye() {
6555-
alert(`Bye ${this.name}`);
6556-
},
6548+
// Define a mixin
6549+
const canEat = {
6550+
eat() {
6551+
console.log("Eating...");
6552+
}
6553+
};
6554+
6555+
const canWalk = {
6556+
walk() {
6557+
console.log("Walking...");
6558+
}
6559+
};
6560+
6561+
const canRead = {
6562+
read() {
6563+
console.log("Reading...");
6564+
}
65576565
};
65586566

6559-
// usage:
6560-
class User {
6567+
// Create a class
6568+
class Person {
65616569
constructor(name) {
65626570
this.name = name;
65636571
}
65646572
}
65656573

6566-
// copy the methods
6567-
Object.assign(User.prototype, cleanRoomMixin);
6574+
// Apply mixins
6575+
Object.assign(Person.prototype, canEat, canWalk, canRead);
65686576

6569-
// now User can clean the room
6570-
new User("Dude").cleanRoom(); // Hello Dude, your room is clean now!
6577+
// Use it
6578+
const person = new Person("Sudheer");
6579+
person.eat(); // Output: Eating...
6580+
person.walk(); // Output: Walking...
6581+
person.read(); // Output: Reading...
65716582
```
6583+
### Benefits
6584+
- Avoids deep inheritance hierarchies
6585+
- Encourages composition over inheritance
6586+
- Promotes reusable and modular code
6587+
6588+
Modern JavaScript favors mixin alternatives like composition, delegation, higher-order functions, and class mixins to promote reusable and modular code. Libraries like Lodash offer utilities for object composition, while frameworks like Vue.js provide built-in mixin features to promote reusable and modular code.
65726589
65736590
**[⬆ Back to Top](#table-of-contents)**
65746591
6575-
336. ### What is a thunk function
6592+
1. ### What is a thunk function
65766593
65776594
A thunk is just a function which delays the evaluation of the value. It doesn’t take any arguments but gives the value whenever you invoke the thunk. i.e, It is used not to execute now but it will be sometime in the future. Let's take a synchronous example,
65786595
@@ -6586,7 +6603,7 @@
65866603
65876604
**[⬆ Back to Top](#table-of-contents)**
65886605
6589-
337. ### What are asynchronous thunks
6606+
2. ### What are asynchronous thunks
65906607
65916608
The asynchronous thunks are useful to make network requests. Let's see an example of network requests,
65926609
@@ -6610,7 +6627,7 @@
66106627
66116628
**[⬆ Back to Top](#table-of-contents)**
66126629
6613-
338. ### What is the output of below function calls
6630+
3. ### What is the output of below function calls
66146631
66156632
**Code snippet:**
66166633
@@ -6635,7 +6652,7 @@
66356652
66366653
**[⬆ Back to Top](#table-of-contents)**
66376654
6638-
339. ### How to remove all line breaks from a string
6655+
4. ### How to remove all line breaks from a string
66396656
66406657
The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.
66416658
@@ -6649,13 +6666,13 @@
66496666
66506667
**[⬆ Back to Top](#table-of-contents)**
66516668
6652-
340. ### What is the difference between reflow and repaint
6669+
5. ### What is the difference between reflow and repaint
66536670
66546671
A _repaint_ occurs when changes are made which affect the visibility of an element, but not its layout. Examples of this include outline, visibility, or background color. A _reflow_ involves changes that affect the layout of a portion of the page (or the whole page). Resizing the browser window, changing the font, content changing (such as user typing text), using JavaScript methods involving computed styles, adding or removing elements from the DOM, and changing an element's classes are a few of the things that can trigger reflow. Reflow of an element causes the subsequent reflow of all child and ancestor elements as well as any elements following it in the DOM.
66556672
66566673
**[⬆ Back to Top](#table-of-contents)**
66576674
6658-
341. ### What happens with negating an array
6675+
6. ### What happens with negating an array
66596676
66606677
Negating an array with `!` character will coerce the array into a boolean. Since Arrays are considered to be truthy So negating it will return `false`.
66616678
@@ -6665,7 +6682,7 @@
66656682
66666683
**[⬆ Back to Top](#table-of-contents)**
66676684
6668-
342. ### What happens if we add two arrays
6685+
7. ### What happens if we add two arrays
66696686
66706687
If you add two arrays together, it will convert them both to strings and concatenate them. For example, the result of adding arrays would be as below,
66716688
@@ -6677,7 +6694,7 @@
66776694
66786695
**[⬆ Back to Top](#table-of-contents)**
66796696
6680-
343. ### What is the output of prepend additive operator on falsy values
6697+
8. ### What is the output of prepend additive operator on falsy values
66816698
66826699
If you prepend the additive(+) operator on falsy values(null, undefined, NaN, false, ""), the falsy value converts to a number value zero. Let's display them on browser console as below,
66836700
@@ -6691,7 +6708,7 @@
66916708
66926709
**[⬆ Back to Top](#table-of-contents)**
66936710
6694-
344. ### How do you create self string using special characters
6711+
9. ### How do you create self string using special characters
66956712
66966713
The self string can be formed with the combination of `[]()!+` characters. You need to remember the below conventions to achieve this pattern.
66976714
@@ -6723,7 +6740,7 @@
67236740
67246741
**[⬆ Back to Top](#table-of-contents)**
67256742
6726-
345. ### How do you remove falsy values from an array
6743+
10. ### How do you remove falsy values from an array
67276744
67286745
You can apply the filter method on the array by passing Boolean as a parameter. This way it removes all falsy values(0, undefined, null, false and "") from the array.
67296746
@@ -6734,7 +6751,7 @@
67346751
67356752
**[⬆ Back to Top](#table-of-contents)**
67366753
6737-
346. ### How do you get unique values of an array
6754+
11. ### How do you get unique values of an array
67386755
67396756
You can get unique values of an array with the combination of `Set` and rest expression/spread(...) syntax.
67406757
@@ -6744,7 +6761,7 @@
67446761
67456762
**[⬆ Back to Top](#table-of-contents)**
67466763
6747-
347. ### What is destructuring aliases
6764+
12. ### What is destructuring aliases
67486765
67496766
Sometimes you would like to have a destructured variable with a different name than the property name. In that case, you'll use a `: newName` to specify a name for the variable. This process is called destructuring aliases.
67506767
@@ -6756,7 +6773,7 @@
67566773
67576774
**[⬆ Back to Top](#table-of-contents)**
67586775
6759-
348. ### How do you map the array values without using map method
6776+
13. ### How do you map the array values without using map method
67606777
67616778
You can map the array values without using the `map` method by just using the `from` method of Array. Let's map city names from Countries array,
67626779
@@ -6798,7 +6815,7 @@
67986815
67996816
**[⬆ Back to Top](#table-of-contents)**
68006817
6801-
366. ### What is the easiest way to convert an array to an object
6818+
351. ### What is the easiest way to convert an array to an object
68026819
68036820
You can convert an array to an object with the same data using spread(...) operator.
68046821
@@ -6810,7 +6827,7 @@
68106827
68116828
**[⬆ Back to Top](#table-of-contents)**
68126829
6813-
367. ### How do you create an array with some data
6830+
352. ### How do you create an array with some data
68146831
68156832
You can create an array with some data or an array with the same values using `fill` method.
68166833
@@ -6821,7 +6838,7 @@
68216838
68226839
**[⬆ Back to Top](#table-of-contents)**
68236840
6824-
368. ### What are the placeholders from console object
6841+
353. ### What are the placeholders from console object
68256842
68266843
Below are the list of placeholders available from console object,
68276844
@@ -6841,7 +6858,7 @@
68416858
68426859
**[⬆ Back to Top](#table-of-contents)**
68436860
6844-
369. ### Is it possible to add CSS to console messages
6861+
354. ### Is it possible to add CSS to console messages
68456862
68466863
Yes, you can apply CSS styles to console messages similar to html text on the web page.
68476864
@@ -6859,7 +6876,7 @@
68596876
68606877
**[⬆ Back to Top](#table-of-contents)**
68616878
6862-
370. ### What is the purpose of dir method of console object
6879+
355. ### What is the purpose of dir method of console object
68636880
68646881
The `console.dir()` is used to display an interactive list of the properties of the specified JavaScript object as JSON.
68656882
@@ -6873,7 +6890,7 @@
68736890
68746891
**[⬆ Back to Top](#table-of-contents)**
68756892
6876-
371. ### Is it possible to debug HTML elements in console
6893+
356. ### Is it possible to debug HTML elements in console
68776894
68786895
Yes, it is possible to get and debug HTML elements in the console just like inspecting elements.
68796896
@@ -6888,7 +6905,7 @@
68886905
68896906
**[⬆ Back to Top](#table-of-contents)**
68906907
6891-
372. ### How do you display data in a tabular format using console object
6908+
357. ### How do you display data in a tabular format using console object
68926909
68936910
The `console.table()` is used to display data in the console in a tabular format to visualize complex arrays or objects.
68946911
@@ -6908,7 +6925,7 @@
69086925
69096926
**[⬆ Back to Top](#table-of-contents)**
69106927
6911-
373. ### How do you verify that an argument is a Number or not
6928+
358. ### How do you verify that an argument is a Number or not
69126929
69136930
The combination of IsNaN and isFinite methods are used to confirm whether an argument is a number or not.
69146931
@@ -6920,7 +6937,7 @@
69206937
69216938
**[⬆ Back to Top](#table-of-contents)**
69226939
6923-
374. ### How do you create copy to clipboard button
6940+
359. ### How do you create copy to clipboard button
69246941
69256942
You need to select the content(using .select() method) of the input element and execute the copy command with execCommand (i.e, execCommand('copy')). You can also execute other system commands like cut and paste.
69266943
@@ -6935,7 +6952,7 @@
69356952
69366953
**[⬆ Back to Top](#table-of-contents)**
69376954
6938-
375. ### What is the shortcut to get timestamp
6955+
360. ### What is the shortcut to get timestamp
69396956
69406957
You can use `new Date().getTime()` to get the current timestamp. There is an alternative shortcut to get the value.
69416958
@@ -6946,7 +6963,7 @@
69466963
69476964
**[⬆ Back to Top](#table-of-contents)**
69486965
6949-
376. ### How do you flattening multi dimensional arrays
6966+
361. ### How do you flattening multi dimensional arrays
69506967
69516968
Flattening bi-dimensional arrays is trivial with Spread operator.
69526969
@@ -6991,7 +7008,7 @@
69917008
69927009
**[⬆ Back to Top](#table-of-contents)**
69937010
6994-
377. ### What is the easiest multi condition checking
7011+
362. ### What is the easiest multi condition checking
69957012
69967013
You can use `indexOf` to compare input with multiple values instead of checking each value as one condition.
69977014
@@ -7013,7 +7030,7 @@
70137030
70147031
**[⬆ Back to Top](#table-of-contents)**
70157032
7016-
378. ### How do you capture browser back button
7033+
363. ### How do you capture browser back button
70177034
70187035
The `beforeunload` event is triggered when the window, the document and its resources are about to be unloaded. This event is helpful to warn users about losing the current data and detect back button event.
70197036

0 commit comments

Comments
 (0)