我有这样一个代码是一个js文件,另一个是HTML文件,问题是,当我运行的代码浏览器的犯规显示功能的文本文件撰写(...),有人能帮助我吗?
function prose(name, campany){
this.name= name;
this.company = company;
this.rate = rate;
}
<!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>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<script>
var y = new prose("JavaScript", "Packt Publishing");
y.rate(50);
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.sellingPrice+ "<br>");
</script>
<!--Ejemplo 5
<a href="javascript:PacktPub()">Click Here</a> -->
</body>
</html>
此代码的工作
<!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>
你有一些语法错误(CAMPANY - >公司,率 - > sellingPrice)。
你有几个错误。首先,您引用company
和你写campany
它不存在。
改成:
function prose(name, company)
其次,prose
是一个函数,不是类,因此它无法识别要调用的new
。为了解决这个问题,要么删除new
或更改它是一类:
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>