在Javascript对象中调用函数

时间:2013-11-26 16:16:46

标签: javascript

我已经定义了一个具有2个属性的对象和一个名为fullname的函数。当我调用函数fullname时,我看到显示了另一行显示。

<html>
    <head>
        <script>
            function person(firstname,lastname){
                this.firstname = firstname;
                this.lastname = lastname;
                var id = 1;
                this.fullname = function(){
                console.log("firstname = " + firstname + " lastname = " + lastname);    
                }
            }

            x = new person("Bob","McDonald");
            console.log(x.firstname);
            console.log(x.fullname());
            console.log("Display finish");

        </script>
    </head>
    <body>
        This is the body
    </body>
</html>

萤火虫的输出 -

  

鲍勃
  basics.html(第14行)firstname = Bob lastname = McDonald
  basics.html(第9行)undefined
  basics.html(第15行)显示完成

1 个答案:

答案 0 :(得分:1)

您的函数fullname已经登录到控制台并且不返回任何内容。你只需要打电话。

console.log(x.firstname);
x.fullname(); //no console.log
console.log("Display finish");