Creating functions in function prototypes for Javascript

BlueElixir

How do I properly create a function within a function prototype? What I have is this:

    <body>
    <p id="demo"></p><script>
function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
person.prototype.name = function() {
    return {
        myFunc: function() {
          this.firstName + " " + this.lastName;
       }
      }
};

var myFather = new person("John", "Doe", 50, "blue");

document.getElementById("demo").innerHTML =
"My father is " + myFather.name().myFunc; 
</script>

</body>

When I run this it returns "My father is function () { this.firstName + " " + this.lastName; }" , but I was expecting John Doe.

Oleksandr T.

You need call function, add () to myFunc. In your example you added reference to internal function.

document.getElementById("demo").innerHTML = "My father is " + myFather.name().myFunc(); 

Also add return to myFunc. To get properties from parent scope - save reference to this

person.prototype.name = function () {
  var _this = this;

  return {
    myFunc: function () {
      return _this.firstName + " " + _this.lastName;
    }
  }
};

Example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript functions and prototypes

From Dev

JavaScript prototypes: replace a function

From Dev

using a group of functions as a prototypes in javascript

From Dev

Is it safe to declare function prototypes for functions that do not exist?

From Dev

Writing functions conforming template defined function prototypes

From Dev

JavaScript - The Good Parts: Function prototypes vs Object prototypes

From Dev

React and Javascript: Equivalent for C function prototypes?

From Dev

ES6 const for creating object prototypes in JavaScript; is this a pattern?

From Dev

Is it a good practice to always have function prototypes for static functions in C?

From Dev

Is this allowed to call functions with different prototypes by a pseudo-generic function pointer?

From Dev

Is this allowed to call functions with different prototypes by a pseudo-generic function pointer?

From Dev

Javascript: Creating Functions in a For Loop

From Dev

Is it necessary to declare prototypes of functions?

From Dev

Modular programming and functions prototypes

From Dev

function prototypes in main function?

From Dev

function prototypes in main function?

From Dev

Different ways of creating functions in javascript

From Dev

Different ways of creating functions in javascript

From Dev

Creating a Button to Function Javascript

From Dev

Creating a callback on javascript function

From Dev

this scope in arrow function prototypes

From Dev

Why use function prototypes?

From Dev

Are there function prototypes in Common Lisp?

From Dev

Function prototypes and argument coercion

From Dev

Why use function prototypes?

From Dev

Understanding of JavaScript Prototypes

From Dev

Javascript nested prototypes

From Dev

Constructor and prototypes in javascript

From Dev

Safely inheriting prototypes in JavaScript

Related Related

  1. 1

    JavaScript functions and prototypes

  2. 2

    JavaScript prototypes: replace a function

  3. 3

    using a group of functions as a prototypes in javascript

  4. 4

    Is it safe to declare function prototypes for functions that do not exist?

  5. 5

    Writing functions conforming template defined function prototypes

  6. 6

    JavaScript - The Good Parts: Function prototypes vs Object prototypes

  7. 7

    React and Javascript: Equivalent for C function prototypes?

  8. 8

    ES6 const for creating object prototypes in JavaScript; is this a pattern?

  9. 9

    Is it a good practice to always have function prototypes for static functions in C?

  10. 10

    Is this allowed to call functions with different prototypes by a pseudo-generic function pointer?

  11. 11

    Is this allowed to call functions with different prototypes by a pseudo-generic function pointer?

  12. 12

    Javascript: Creating Functions in a For Loop

  13. 13

    Is it necessary to declare prototypes of functions?

  14. 14

    Modular programming and functions prototypes

  15. 15

    function prototypes in main function?

  16. 16

    function prototypes in main function?

  17. 17

    Different ways of creating functions in javascript

  18. 18

    Different ways of creating functions in javascript

  19. 19

    Creating a Button to Function Javascript

  20. 20

    Creating a callback on javascript function

  21. 21

    this scope in arrow function prototypes

  22. 22

    Why use function prototypes?

  23. 23

    Are there function prototypes in Common Lisp?

  24. 24

    Function prototypes and argument coercion

  25. 25

    Why use function prototypes?

  26. 26

    Understanding of JavaScript Prototypes

  27. 27

    Javascript nested prototypes

  28. 28

    Constructor and prototypes in javascript

  29. 29

    Safely inheriting prototypes in JavaScript

HotTag

Archive