Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我在使用document.write()时未定义?_Javascript_Oop - Fatal编程技术网

Javascript 为什么我在使用document.write()时未定义?

Javascript 为什么我在使用document.write()时未定义?,javascript,oop,Javascript,Oop,为什么在使用document.write时在浏览器中未定义 <script type="text/javascript"> function Person (name) { this.name = name; this.greeting = function() { document.write("Hello " + name + " Go rock the industry!!"); }; }

为什么在使用document.write时在浏览器中未定义

<script type="text/javascript">
    function Person (name) {
        this.name = name;
        this.greeting = function() {
            document.write("Hello " + name +  " Go rock the industry!!");
        };
    }

    var person1 = new Person("Sara");
    var person2 = new Person("Bob");

    document.write(person1.name + "<br>");
    document.write(person1.greeting() + "<br>");
    document.write(person2.name + "<br>");
    document.write(person2.greeting() + "<br>");

</script>
您正在尝试对一个函数使用document.write,该函数不返回值,而是执行document.write本身。Person.greeting应返回一个值,以便在调用document.writeperson2.greeting时使用该值

应该是这样的:

function Person (name) {
    this.name = name;
    this.greeting = function() {
        return ("Hello " + name +  " Go rock the industry!!");
          ▲
    };
}
您需要更新问候语函数以返回字符串,而不是调用document.write本身

当前,问候语函数正在调用document.write,然后隐式返回未定义的值

当您计算表达式时

personX.greeting+

它评估为

未定义+

并且,根据JavaScript字符串连接的规则,undefined首先被转换为undefined字符串,然后与下一个值连接

编辑:

如果您对探索感兴趣,还可以根据需要进行重构

function Person (name) {
    this.name = name;
    Object.defineProperty(this, 'greeting', {
        get: function() {
            return "Hello " + this.name +  " Go rock the industry!!";
        }
    });
}

var person1 = new Person("Sara");
var person2 = new Person("Bob");

document.write(person1.name + "<br>");
document.write(person1.greeting + "<br>");
document.write(person2.name + "<br>");
document.write(person2.greeting + "<br>");

document.write返回未定义。您正在使用document.write-in-document.write。相反,只需从函数return'Hello'+name返回字符串……最好在Person prototype上定义问候语方法,并将name引用为this.name,而不要使用document.write在body上添加内容。@Tushar您的意思是喜欢这个函数Person name吗{this.name=name;this.greeting=function{document.writeHello+this.name+Go-rock-the-industry!!;};}var person1=new PersonSara;document.writeperson1.name+;person1.greeting;@rockstone请查看我的answer@Tushar好的,它工作正常::抱歉,但是在超过15+后声誉会下降。我一定会这样做。我可以这样做吗:函数人名{this.name=name;this.greeting=function{document.writeHello+this.name+Go-rock-the-industry!!;};}var person1=new PersonSara;document.writeperson1.name+;person1.greeting;这也会起作用,是的。但是,它会在对象的工作方式上产生一种奇怪的不一致性;如果这样做,可能会将greeting方法重命名为writeGreeting或其他什么,这样您和任何其他开发人员都会知道它正在向文档写入,而不是返回字符串。
function Person (name) {
    this.name = name;
    Object.defineProperty(this, 'greeting', {
        get: function() {
            return "Hello " + this.name +  " Go rock the industry!!";
        }
    });
}

var person1 = new Person("Sara");
var person2 = new Person("Bob");

document.write(person1.name + "<br>");
document.write(person1.greeting + "<br>");
document.write(person2.name + "<br>");
document.write(person2.greeting + "<br>");
class Person {

  constructor(name) {
    this.name = name;
  }

  get greeting() {
    return `Hello ${this.name} Go rock the industry!!`
  }
}

let person1 = new Person('Sara');
let person2 = new Person('Bob');

document.write(person1.name + "<br>");
document.write(person1.greeting + "<br>");
document.write(person2.name + "<br>");
document.write(person2.greeting + "<br>");