1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>oop</title>

    <script charset="utf-8" type="text/javascript">

        function Person() {

        }

        Person.prototype = {
            name : 'Lucy',
            age : 23,
            friends : ['Kinsey','Godwin','David'],
            sayHello: function () {
                document.write('hi,boy' + '<br><br>');
            }
        };

        var p = new Person();
        var p2= new Person();
        document.write(p.sayHello());

        p.friends.push('Berkhoom');
        document.write(p.friends + '<br><br>');
        document.write(p2.friends + '<br><br>');

    </script>


</head>
<body>

</body>
</html>

**hi,boy

undefinedKinsey,Godwin,David,Berkhoom

Kinsey,Godwin,David,Berkhoom **

I don't kown where the word undefined comes from? help?

the program and result.

3
  • the output result:hi,boy undefinedKinsey,Godwin,David,Berkhoom Kinsey,Godwin,David,Berkhoom Commented Apr 4, 2017 at 10:42
  • Check your p.fiends array: console.log(p.friends). I think you have first element empty (removed it?) Commented Apr 4, 2017 at 10:45
  • 3
    undefined is the standard return type for a function, like sayHello. You don't need to write the output of sayHello. Replace document.write(p.sayHello()); with p.sayHello(); Commented Apr 4, 2017 at 10:47

2 Answers 2

1

In your actual code undefined comes from the line:

document.write(p.sayHello());

Because undefined is the default return for a function, and you haven't specified any return for your sayHello function.

This is a snippet where, I commented the above line showing the difference:

        function Person() {

        }

        Person.prototype = {
            name : 'Lucy',
            age : 23,
            friends : ['Kinsey','Godwin','David'],
            sayHello: function () {
                document.write('hi,boy' + '<br><br>');
            }
        };

        var p = new Person();
        var p2= new Person();
        //document.write(p.sayHello());

        p.friends.push('Berkhoom');
        document.write(p.friends + '<br><br>');
        document.write(p2.friends + '<br><br>');

So you just need to call the function sayHello() instead of writing its output:

function Person() {

}

Person.prototype = {
  name: 'Lucy',
  age: 23,
  friends: ['Kinsey', 'Godwin', 'David'],
  sayHello: function() {
    document.write('hi,boy' + '<br><br>');
  }
};

var p = new Person();
var p2 = new Person();
p.sayHello();

p.friends.push('Berkhoom');
document.write(p.friends + '<br><br>');
document.write(p2.friends + '<br><br>');

Sign up to request clarification or add additional context in comments.

1 Comment

@GodwinQ You are welcome. can you please accept my answer as it helps? Thank you
0
document.write(p.sayHello());

this is printing undefined
you don't need to call document.write() here just call:

p.sayHello();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.