How can I add a property calculated by a JavaScript extension with C#?

padavan

I have array and want access by property (not function) like:

let arr = [1,2,3];
console.log("last element: ", arr.last)

I try like it, but got error:

Array.prototype.last = (() => {
    return this[this.length - 1];
})();

Update 1: yes i know how do it like extend method:

 Array.prototype.mysort = function() {
        this.sort(() => Math.random() - 0.5);
        return this;
    }
    myarray.mysort();

but how do it like property?

myarray.mysort;
Barmar

You can do this with a getter. You need to use Object.defineProperty() to add a getter to an existing object, in this case the Array.prototype object.

Object.defineProperty(Array.prototype, "last", {
  get: function() {
    return this[this.length - 1];
  }
});

const a = [1, 2, 3];
console.log(a.last);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I add start at a cetrain height property to my JavaScript?

From Dev

How can I add a calculated column to a Pandas dataframe?

From Dev

How can I add a calculated column with different rows to a dataframe?

From Dev

How can I add a custom fee in woocommerce that is calculated after tax

From Dev

How can i add an extension to Powershell?

From Dev

How to add custom data as calculated property in Powershell

From Dev

How can I add this property to JsonConvert?

From Dev

How can I create the days property in a DateInterval extension class?

From Dev

Javascript why can't i use an array like this and how can i access the array with a calculated number

From Dev

How can I solve this Null property on Javascript?

From Dev

how can i read a property of an object in javascript

From Dev

How do I create an extension property in C#?

From Dev

How can I add a context menu to a browser action with a Chrome extension?

From Dev

How can I add a function extension for Fragment class in Kotlin?

From Dev

How can I add an option to the context menu dynamically in a Chrome extension?

From Dev

How can I add these code completion features my VSCode extension?

From

How can I add an extension method to many classes?

From Dev

How can I add a Chrome extension listener both onStartup and onInstalled?

From Dev

How can I add a context menu extension for the root directory?

From Dev

How can I add a function to Swift as an extension to String to simplify usage

From Dev

How can I add a bulma extension to a nuxt.js project?

From Dev

How can I add an EXTERNAL extension into selenium (chrome) python

From Dev

How can I add a custom chrome extension to my Electron app?

From Dev

How can I add an extension that uses the internal Gollum markdown processor?

From Dev

How can I add a button to a window for a Visual Studio Extension?

From Dev

How can I add redux-saga to my web extension?

From Dev

C# Outlook Add-in: How can I delete a User-defined property programmatically?

From Dev

How can I add four calculated values to a result set from a Stored Proc (TSQL)?

From Dev

How can I add a new calculated column using a window function to my SQL query?

Related Related

  1. 1

    How can I add start at a cetrain height property to my JavaScript?

  2. 2

    How can I add a calculated column to a Pandas dataframe?

  3. 3

    How can I add a calculated column with different rows to a dataframe?

  4. 4

    How can I add a custom fee in woocommerce that is calculated after tax

  5. 5

    How can i add an extension to Powershell?

  6. 6

    How to add custom data as calculated property in Powershell

  7. 7

    How can I add this property to JsonConvert?

  8. 8

    How can I create the days property in a DateInterval extension class?

  9. 9

    Javascript why can't i use an array like this and how can i access the array with a calculated number

  10. 10

    How can I solve this Null property on Javascript?

  11. 11

    how can i read a property of an object in javascript

  12. 12

    How do I create an extension property in C#?

  13. 13

    How can I add a context menu to a browser action with a Chrome extension?

  14. 14

    How can I add a function extension for Fragment class in Kotlin?

  15. 15

    How can I add an option to the context menu dynamically in a Chrome extension?

  16. 16

    How can I add these code completion features my VSCode extension?

  17. 17

    How can I add an extension method to many classes?

  18. 18

    How can I add a Chrome extension listener both onStartup and onInstalled?

  19. 19

    How can I add a context menu extension for the root directory?

  20. 20

    How can I add a function to Swift as an extension to String to simplify usage

  21. 21

    How can I add a bulma extension to a nuxt.js project?

  22. 22

    How can I add an EXTERNAL extension into selenium (chrome) python

  23. 23

    How can I add a custom chrome extension to my Electron app?

  24. 24

    How can I add an extension that uses the internal Gollum markdown processor?

  25. 25

    How can I add a button to a window for a Visual Studio Extension?

  26. 26

    How can I add redux-saga to my web extension?

  27. 27

    C# Outlook Add-in: How can I delete a User-defined property programmatically?

  28. 28

    How can I add four calculated values to a result set from a Stored Proc (TSQL)?

  29. 29

    How can I add a new calculated column using a window function to my SQL query?

HotTag

Archive