Javascript 为什么使用document.write不';不在浏览器上显示文本

Javascript 为什么使用document.write不';不在浏览器上显示文本,javascript,html,document.write,Javascript,Html,Document.write,我有这段代码一个是js文件,另一个是html文件,问题是当我运行代码时浏览器没有显示函数文档中的文本。写(…),有人能帮我吗 函数散文(名称,campany){ this.name=name; 这个公司=公司; 这个比率=比率; } 文件 var y=新散文(“JavaScript”、“Packt Publishing”); y、 比率(50); 文件。书写(“书名为“+y.name+””; 文件。书写(“出版公司名称为:“+y.company+””; 文件。书写(“本书的成本为“$”+y.

我有这段代码一个是js文件,另一个是html文件,问题是当我运行代码时浏览器没有显示函数文档中的文本。写(…),有人能帮我吗

函数散文(名称,campany){
this.name=name;
这个公司=公司;
这个比率=比率;
}

文件
var y=新散文(“JavaScript”、“Packt Publishing”);
y、 比率(50);
文件。书写(“书名为“+y.name+”
”; 文件。书写(“出版公司名称为:“+y.company+”
”; 文件。书写(“本书的成本为“$”+y.sellingPrice+”
”;
此代码有效

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

  </head>

  <body>

    <script>
      function prose(name, company) {
        this.name = name,
          this.company = company,
          this.rate = 0
      }

      var y = new prose("JavaScript", "Packet Publishing");
      y.rate = 30;
      document.write("The name of the book is " + y.name + "<br>");
      document.write("The name of the publishing company is : " + y.company + "<br>");
      document.write("The cost of the book is $" + y.rate + "<br>");

    </script>

    <!--Ejemplo 5
    <a href="javascript:PacktPub()">Click Here</a> -->
  </body>

</html>

文件
职能(名称、公司){
this.name=name,
这个公司,
这个比率=0
}
var y=新散文(“JavaScript”,“数据包发布”);
y、 比率=30;
文件。书写(“书名为“+y.name+”
”; 文件。书写(“出版公司名称为:“+y.company+”
”; 文件。书写(“这本书的成本是$”+y.rate+“
”);
您有一些语法错误(campany->company,rate->sellingPrice)。

此代码有效

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

  </head>

  <body>

    <script>
      function prose(name, company) {
        this.name = name,
          this.company = company,
          this.rate = 0
      }

      var y = new prose("JavaScript", "Packet Publishing");
      y.rate = 30;
      document.write("The name of the book is " + y.name + "<br>");
      document.write("The name of the publishing company is : " + y.company + "<br>");
      document.write("The cost of the book is $" + y.rate + "<br>");

    </script>

    <!--Ejemplo 5
    <a href="javascript:PacktPub()">Click Here</a> -->
  </body>

</html>

文件
职能(名称、公司){
this.name=name,
这个公司,
这个比率=0
}
var y=新散文(“JavaScript”,“数据包发布”);
y、 比率=30;
文件。书写(“书名为“+y.name+”
”; 文件。书写(“出版公司名称为:“+y.company+”
”; 文件。书写(“这本书的成本是$”+y.rate+“
”);
您有一些语法错误(campany->company,rate->sellingPrice)。

您有几个错误。 首先,您引用的是
公司
,它在您编写
坎帕尼
时并不存在

改为:

函数散文(名称、公司)

其次,
pross
是一个函数,而不是一个类,因此它无法识别您正在调用的
新的
。要解决此问题,请删除
新的
或将其更改为类:

let prose = (() =>{

    function prose(name, company, rate) {
        this.name= name;
        this.company = company;
        this.rate = rate; 
    }

    return prose;

})();
同时也修正了你设定利率的方式

y.rate = 50;
你有几个错误。 首先,您引用的是
公司
,它在您编写
坎帕尼
时并不存在

改为:

函数散文(名称、公司)

其次,
pross
是一个函数,而不是一个类,因此它无法识别您正在调用的
新的
。要解决此问题,请删除
新的
或将其更改为类:

let prose = (() =>{

    function prose(name, company, rate) {
        this.name= name;
        this.company = company;
        this.rate = rate; 
    }

    return prose;

})();
同时也修正了你设定利率的方式

y.rate = 50;

这应该可以解释一切

<body>
  <script>
    // Defines an object prototype called prose
    function prose(name, company){
      // Any parameter names used inside the constructor must exist in its
      //  signature above
      this.name = name;
      this.company = company;
      // prose.rate is initially set to an empty string because no rate 
      //  argument is passed in 
      this.rate = "";
    }

    // Constructs a new object using the `prose` prototype. (Note that 
    //   JS now also has 'class' syntax available.)
    const y = new prose("JavaScript", "Packt Publishing"); 
       // It would be better to use a more descriptive identifier than `y`

    // Assigns a new value to the rate property of the new object.
    y.rate = 50;


    // We will not use `document.write()` because it replaces all 
    //  existing page content each time it is used
    // The paragraph element (and others) could be included in static 
    //  HTML instead of added dynamically using javascript as we do here

    // Defines nodes to add to the document
    const myParagraph = document.createElement("p");
    const nameText = document.createTextNode("The name of the book is: " + y.name);
    const companyText = document.createTextNode("The name of the publishing company is: " + y.company);
    //priceText includes a fallback string in case y.sellingPrice does not exist
    const priceText = document.createTextNode("The selling price of the book is: " + (y.sellingPrice || "(unknown)"));
    const rateText = document.createTextNode("The rate of the book is: " + y.rate);

    // This function can be called repeatedly to create & append a line break
    function appendLineBreakTo(parentElement){
      const lineBreak = document.createElement("br");
      parentElement.appendChild(lineBreak);
    }

    // Adds all the text nodes inside the newly created paragraph element
    myParagraph.appendChild(nameText);
    appendLineBreakTo(myParagraph); //Calls function to add <br/>

    myParagraph.appendChild(companyText);
    appendLineBreakTo(myParagraph);

    myParagraph.appendChild(priceText);
    appendLineBreakTo(myParagraph);

    myParagraph.appendChild(rateText);
    appendLineBreakTo(myParagraph);

    // Gets a reference to an HTML element that already exists in the DOM
    const body = document.querySelector("body");

    // Adds the paragraph element (with all of its new text nodes) to 
    //  the existing element
    body.appendChild(myParagraph);
  </script>
</body>

//定义一个名为散文的对象原型
职能(名称、公司){
//构造函数中使用的任何参数名称都必须存在于其
//上面签名
this.name=名称;
这个公司=公司;
//Pross.rate最初设置为空字符串,因为没有速率
//参数被传入
此值为“=”;
}
//使用“散文”原型构造新对象。(注意
//JS现在还提供了“类”语法。)
const y=新散文(“JavaScript”,“Packt Publishing”);
//最好使用比“y”更具描述性的标识符`
//将新值指定给新对象的“速率”特性。
y、 比率=50;
//我们不会使用“document.write()”,因为它会替换所有
//每次使用现有页面内容时
//段落元素(和其他元素)可以包含在静态
//HTML,而不是像我们在这里所做的那样使用javascript动态添加
//定义要添加到文档中的节点
const my段落=document.createElement(“p”);
const nameText=document.createTextNode(“书的名称为:”+y.name);
const companyText=document.createTextNode(“发布公司的名称为:“+y.company”);
//priceText在y.sellingPrice不存在的情况下包含回退字符串
const priceText=document.createTextNode(“该书的售价为:+(y.sellingPrice | |(未知)”);
const rateText=document.createTextNode(“书本的速率为:”+y.rate);
//可以重复调用此函数以创建和追加换行符
函数appendLineBreakTo(parentElement){
const lineBreak=document.createElement(“br”);
parentElement.appendChild(换行符);
}
//在新创建的段落元素中添加所有文本节点
我的段落.appendChild(nameText);
附件(我的段落)//调用要添加的函数
我的段落.appendChild(companyText); 附件(我的段落); 我的段落.appendChild(价格文本); 附件(我的段落); 我的段落.appendChild(文本); 附件(我的段落); //获取对DOM中已存在的HTML元素的引用 const body=document.querySelector(“body”); //将段落元素(及其所有新文本节点)添加到 //现有要素 正文.附件(我的段落);
这应该可以解释一切

<body>
  <script>
    // Defines an object prototype called prose
    function prose(name, company){
      // Any parameter names used inside the constructor must exist in its
      //  signature above
      this.name = name;
      this.company = company;
      // prose.rate is initially set to an empty string because no rate 
      //  argument is passed in 
      this.rate = "";
    }

    // Constructs a new object using the `prose` prototype. (Note that 
    //   JS now also has 'class' syntax available.)
    const y = new prose("JavaScript", "Packt Publishing"); 
       // It would be better to use a more descriptive identifier than `y`

    // Assigns a new value to the rate property of the new object.
    y.rate = 50;


    // We will not use `document.write()` because it replaces all 
    //  existing page content each time it is used
    // The paragraph element (and others) could be included in static 
    //  HTML instead of added dynamically using javascript as we do here

    // Defines nodes to add to the document
    const myParagraph = document.createElement("p");
    const nameText = document.createTextNode("The name of the book is: " + y.name);
    const companyText = document.createTextNode("The name of the publishing company is: " + y.company);
    //priceText includes a fallback string in case y.sellingPrice does not exist
    const priceText = document.createTextNode("The selling price of the book is: " + (y.sellingPrice || "(unknown)"));
    const rateText = document.createTextNode("The rate of the book is: " + y.rate);

    // This function can be called repeatedly to create & append a line break
    function appendLineBreakTo(parentElement){
      const lineBreak = document.createElement("br");
      parentElement.appendChild(lineBreak);
    }

    // Adds all the text nodes inside the newly created paragraph element
    myParagraph.appendChild(nameText);
    appendLineBreakTo(myParagraph); //Calls function to add <br/>

    myParagraph.appendChild(companyText);
    appendLineBreakTo(myParagraph);

    myParagraph.appendChild(priceText);
    appendLineBreakTo(myParagraph);

    myParagraph.appendChild(rateText);
    appendLineBreakTo(myParagraph);

    // Gets a reference to an HTML element that already exists in the DOM
    const body = document.querySelector("body");

    // Adds the paragraph element (with all of its new text nodes) to 
    //  the existing element
    body.appendChild(myParagraph);
  </script>
</body>

//定义一个名为散文的对象原型
职能(名称、公司){
//构造函数中使用的任何参数名称都必须存在于其
//上面签名
this.name=名称;
这个公司=公司;
//Pross.rate最初设置为空字符串,因为没有速率
//参数被传入
此值为“=”;
}
//使用“散文”原型构造新对象。(注意
//JS现在还提供了“类”语法。)
const y=新散文(“JavaScript”,