[Vuejs]-How can I display function content in vue?

1πŸ‘

βœ…

Vue compiles the method definition into Function instances, which don’t expose their source code as strings. And this.METHODNAME gets the compiled Function instance.

However, you could access the original method definition through this.$options.methods.METHODNAME:

new Vue({
  computed: {
    showFunction() {
      return this.$options.methods.confirm πŸ‘ˆ
    }
  },
  methods: {
    confirm() {/*...*/}
  }
})

demo

πŸ‘€tony19

3πŸ‘

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString

If the toString() method is called on built-in function objects or a function created by Function.prototype.bind, toString() returns a native function string which looks like

"function () {\n    [native code]\n}"

Do you have somewhere in your code:

this.confirm = this.confirm.bind(this);

You would need to store the content of the function before binding it.

let myFunction = this.confirm.toString();
this.confirm = this.confirm.bind(this);
πŸ‘€Pascal

Leave a comment