Solution Review: Injecting Multiple Properties
Let's learn the solution to the injecting multiple properties challenge.
We'll cover the following...
We'll cover the following...
Solution
Let’s start understanding the solution code.
Node.js
'use strict';Object.defineProperties(Number.prototype, {integerPart: {get: function() {return this.toString().split('.')[0];}},fractionalPart: {get: function() { return this.toString().split('.')[1] || 0; }}});const printParts = function(number) {return `whole: ${number.integerPart} decimal: ${number.fractionalPart}`;};console.log(printParts(22.12)); //whole: 22 decimal: 12console.log(printParts(.14)); //whole: 0 decimal: 14console.log(printParts(-23.19)); //whole: -23 decimal: 19console.log(printParts(42)); //whole: 42 decimal: 0
Explanation
For injection properties, we use a special defineProperties
method.
The defineProperties()
method takes only two parameters:
- The candidate object for injection
- An object with properties as