LestaD/nonstandard.js

View on GitHub
src/array/properties.js

Summary

Maintainability
A
2 hrs
Test Coverage
const { defineProperty } = require('../tools');

module.exports.Install = function Install() {
  defineProperty(Array.prototype, 'first',
    function() {
      return this[0];
    },
    function(value) {
      this[0] = value;
      return value;
    }
  );
  

  defineProperty(Array.prototype, 'second',
    function() {
      return this[1];
    },
    function(value) {
      this[1] = value;
      return value;
    }
  );


  defineProperty(Array.prototype, 'last',
    function() {
      if (this.length === 0) return undefined;
      return this[this.length - 1];
    },
    function(value) {
      if (this.length > 0) {
        this[this.length - 1] = value;
        return value;
      }
      return undefined;
    }
  );
}