|
6539 | 6539 |
|
6540 | 6540 | 335. ### Does javascript uses mixins
|
6541 | 6541 |
|
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. |
6543 | 6543 |
|
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. |
6547 | 6545 |
|
| 6546 | + ### Mixin Example using Object composition |
6548 | 6547 | ```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 | + } |
6557 | 6565 | };
|
6558 | 6566 |
|
6559 |
| - // usage: |
6560 |
| - class User { |
| 6567 | + // Create a class |
| 6568 | + class Person { |
6561 | 6569 | constructor(name) {
|
6562 | 6570 | this.name = name;
|
6563 | 6571 | }
|
6564 | 6572 | }
|
6565 | 6573 |
|
6566 |
| - // copy the methods |
6567 |
| - Object.assign(User.prototype, cleanRoomMixin); |
| 6574 | + // Apply mixins |
| 6575 | + Object.assign(Person.prototype, canEat, canWalk, canRead); |
6568 | 6576 |
|
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... |
6571 | 6582 | ```
|
| 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. |
6572 | 6589 |
|
6573 | 6590 | **[⬆ Back to Top](#table-of-contents)**
|
6574 | 6591 |
|
6575 |
| -336. ### What is a thunk function |
| 6592 | +1. ### What is a thunk function |
6576 | 6593 |
|
6577 | 6594 | 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,
|
6578 | 6595 |
|
|
6586 | 6603 |
|
6587 | 6604 | **[⬆ Back to Top](#table-of-contents)**
|
6588 | 6605 |
|
6589 |
| -337. ### What are asynchronous thunks |
| 6606 | +2. ### What are asynchronous thunks |
6590 | 6607 |
|
6591 | 6608 | The asynchronous thunks are useful to make network requests. Let's see an example of network requests,
|
6592 | 6609 |
|
|
6610 | 6627 |
|
6611 | 6628 | **[⬆ Back to Top](#table-of-contents)**
|
6612 | 6629 |
|
6613 |
| -338. ### What is the output of below function calls |
| 6630 | +3. ### What is the output of below function calls |
6614 | 6631 |
|
6615 | 6632 | **Code snippet:**
|
6616 | 6633 |
|
|
6635 | 6652 |
|
6636 | 6653 | **[⬆ Back to Top](#table-of-contents)**
|
6637 | 6654 |
|
6638 |
| -339. ### How to remove all line breaks from a string |
| 6655 | +4. ### How to remove all line breaks from a string |
6639 | 6656 |
|
6640 | 6657 | 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.
|
6641 | 6658 |
|
|
6649 | 6666 |
|
6650 | 6667 | **[⬆ Back to Top](#table-of-contents)**
|
6651 | 6668 |
|
6652 |
| -340. ### What is the difference between reflow and repaint |
| 6669 | +5. ### What is the difference between reflow and repaint |
6653 | 6670 |
|
6654 | 6671 | 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.
|
6655 | 6672 |
|
6656 | 6673 | **[⬆ Back to Top](#table-of-contents)**
|
6657 | 6674 |
|
6658 |
| -341. ### What happens with negating an array |
| 6675 | +6. ### What happens with negating an array |
6659 | 6676 |
|
6660 | 6677 | 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`.
|
6661 | 6678 |
|
|
6665 | 6682 |
|
6666 | 6683 | **[⬆ Back to Top](#table-of-contents)**
|
6667 | 6684 |
|
6668 |
| -342. ### What happens if we add two arrays |
| 6685 | +7. ### What happens if we add two arrays |
6669 | 6686 |
|
6670 | 6687 | 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,
|
6671 | 6688 |
|
|
6677 | 6694 |
|
6678 | 6695 | **[⬆ Back to Top](#table-of-contents)**
|
6679 | 6696 |
|
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 |
6681 | 6698 |
|
6682 | 6699 | 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,
|
6683 | 6700 |
|
|
6691 | 6708 |
|
6692 | 6709 | **[⬆ Back to Top](#table-of-contents)**
|
6693 | 6710 |
|
6694 |
| -344. ### How do you create self string using special characters |
| 6711 | +9. ### How do you create self string using special characters |
6695 | 6712 |
|
6696 | 6713 | The self string can be formed with the combination of `[]()!+` characters. You need to remember the below conventions to achieve this pattern.
|
6697 | 6714 |
|
|
6723 | 6740 |
|
6724 | 6741 | **[⬆ Back to Top](#table-of-contents)**
|
6725 | 6742 |
|
6726 |
| -345. ### How do you remove falsy values from an array |
| 6743 | +10. ### How do you remove falsy values from an array |
6727 | 6744 |
|
6728 | 6745 | 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.
|
6729 | 6746 |
|
|
6734 | 6751 |
|
6735 | 6752 | **[⬆ Back to Top](#table-of-contents)**
|
6736 | 6753 |
|
6737 |
| -346. ### How do you get unique values of an array |
| 6754 | +11. ### How do you get unique values of an array |
6738 | 6755 |
|
6739 | 6756 | You can get unique values of an array with the combination of `Set` and rest expression/spread(...) syntax.
|
6740 | 6757 |
|
|
6744 | 6761 |
|
6745 | 6762 | **[⬆ Back to Top](#table-of-contents)**
|
6746 | 6763 |
|
6747 |
| -347. ### What is destructuring aliases |
| 6764 | +12. ### What is destructuring aliases |
6748 | 6765 |
|
6749 | 6766 | 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.
|
6750 | 6767 |
|
|
6756 | 6773 |
|
6757 | 6774 | **[⬆ Back to Top](#table-of-contents)**
|
6758 | 6775 |
|
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 |
6760 | 6777 |
|
6761 | 6778 | 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,
|
6762 | 6779 |
|
|
6798 | 6815 |
|
6799 | 6816 | **[⬆ Back to Top](#table-of-contents)**
|
6800 | 6817 |
|
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 |
6802 | 6819 |
|
6803 | 6820 | You can convert an array to an object with the same data using spread(...) operator.
|
6804 | 6821 |
|
|
6810 | 6827 |
|
6811 | 6828 | **[⬆ Back to Top](#table-of-contents)**
|
6812 | 6829 |
|
6813 |
| -367. ### How do you create an array with some data |
| 6830 | +352. ### How do you create an array with some data |
6814 | 6831 |
|
6815 | 6832 | You can create an array with some data or an array with the same values using `fill` method.
|
6816 | 6833 |
|
|
6821 | 6838 |
|
6822 | 6839 | **[⬆ Back to Top](#table-of-contents)**
|
6823 | 6840 |
|
6824 |
| -368. ### What are the placeholders from console object |
| 6841 | +353. ### What are the placeholders from console object |
6825 | 6842 |
|
6826 | 6843 | Below are the list of placeholders available from console object,
|
6827 | 6844 |
|
|
6841 | 6858 |
|
6842 | 6859 | **[⬆ Back to Top](#table-of-contents)**
|
6843 | 6860 |
|
6844 |
| -369. ### Is it possible to add CSS to console messages |
| 6861 | +354. ### Is it possible to add CSS to console messages |
6845 | 6862 |
|
6846 | 6863 | Yes, you can apply CSS styles to console messages similar to html text on the web page.
|
6847 | 6864 |
|
|
6859 | 6876 |
|
6860 | 6877 | **[⬆ Back to Top](#table-of-contents)**
|
6861 | 6878 |
|
6862 |
| -370. ### What is the purpose of dir method of console object |
| 6879 | +355. ### What is the purpose of dir method of console object |
6863 | 6880 |
|
6864 | 6881 | The `console.dir()` is used to display an interactive list of the properties of the specified JavaScript object as JSON.
|
6865 | 6882 |
|
|
6873 | 6890 |
|
6874 | 6891 | **[⬆ Back to Top](#table-of-contents)**
|
6875 | 6892 |
|
6876 |
| -371. ### Is it possible to debug HTML elements in console |
| 6893 | +356. ### Is it possible to debug HTML elements in console |
6877 | 6894 |
|
6878 | 6895 | Yes, it is possible to get and debug HTML elements in the console just like inspecting elements.
|
6879 | 6896 |
|
|
6888 | 6905 |
|
6889 | 6906 | **[⬆ Back to Top](#table-of-contents)**
|
6890 | 6907 |
|
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 |
6892 | 6909 |
|
6893 | 6910 | The `console.table()` is used to display data in the console in a tabular format to visualize complex arrays or objects.
|
6894 | 6911 |
|
|
6908 | 6925 |
|
6909 | 6926 | **[⬆ Back to Top](#table-of-contents)**
|
6910 | 6927 |
|
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 |
6912 | 6929 |
|
6913 | 6930 | The combination of IsNaN and isFinite methods are used to confirm whether an argument is a number or not.
|
6914 | 6931 |
|
|
6920 | 6937 |
|
6921 | 6938 | **[⬆ Back to Top](#table-of-contents)**
|
6922 | 6939 |
|
6923 |
| -374. ### How do you create copy to clipboard button |
| 6940 | +359. ### How do you create copy to clipboard button |
6924 | 6941 |
|
6925 | 6942 | 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.
|
6926 | 6943 |
|
|
6935 | 6952 |
|
6936 | 6953 | **[⬆ Back to Top](#table-of-contents)**
|
6937 | 6954 |
|
6938 |
| -375. ### What is the shortcut to get timestamp |
| 6955 | +360. ### What is the shortcut to get timestamp |
6939 | 6956 |
|
6940 | 6957 | You can use `new Date().getTime()` to get the current timestamp. There is an alternative shortcut to get the value.
|
6941 | 6958 |
|
|
6946 | 6963 |
|
6947 | 6964 | **[⬆ Back to Top](#table-of-contents)**
|
6948 | 6965 |
|
6949 |
| -376. ### How do you flattening multi dimensional arrays |
| 6966 | +361. ### How do you flattening multi dimensional arrays |
6950 | 6967 |
|
6951 | 6968 | Flattening bi-dimensional arrays is trivial with Spread operator.
|
6952 | 6969 |
|
|
6991 | 7008 |
|
6992 | 7009 | **[⬆ Back to Top](#table-of-contents)**
|
6993 | 7010 |
|
6994 |
| -377. ### What is the easiest multi condition checking |
| 7011 | +362. ### What is the easiest multi condition checking |
6995 | 7012 |
|
6996 | 7013 | You can use `indexOf` to compare input with multiple values instead of checking each value as one condition.
|
6997 | 7014 |
|
|
7013 | 7030 |
|
7014 | 7031 | **[⬆ Back to Top](#table-of-contents)**
|
7015 | 7032 |
|
7016 |
| -378. ### How do you capture browser back button |
| 7033 | +363. ### How do you capture browser back button |
7017 | 7034 |
|
7018 | 7035 | 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.
|
7019 | 7036 |
|
|
0 commit comments