Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.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 为什么函数原型不能在这种情况下工作?_Javascript_Html_Prototype_Prototype Programming_Function Prototypes - Fatal编程技术网

Javascript 为什么函数原型不能在这种情况下工作?

Javascript 为什么函数原型不能在这种情况下工作?,javascript,html,prototype,prototype-programming,function-prototypes,Javascript,Html,Prototype,Prototype Programming,Function Prototypes,我试图使用javascript函数原型打印vehicle对象的值 函数cardetals(){ var车辆=新车(“红色”、“汽车”); 汽车价格(100); //定义将用作方法的函数 vehicle.prototype.price=函数getPrice(金额){ 汽车价格=金额; }; document.getElementById(“color”).innerHTML=car.color; document.getElementById(“type”).innerHTML=car.type

我试图使用javascript函数原型打印
vehicle
对象的值

函数cardetals(){
var车辆=新车(“红色”、“汽车”);
汽车价格(100);
//定义将用作方法的函数
vehicle.prototype.price=函数getPrice(金额){
汽车价格=金额;
};
document.getElementById(“color”).innerHTML=car.color;
document.getElementById(“type”).innerHTML=car.type;
document.getElementById(“price”).innerHTML=car.price;
}
功能车辆(颜色、类型){
这个颜色=颜色;
this.type=type;
//this.prototype.price=getPrice;
}
vehicle.prototype.getPrice=getPrice;
cardetals()

Javascript函数原型

该属性在中为
price
,您可以使用
getPrice()


另外,在prototype上设置set functions,以避免每次构造函数调用都创建functions对象。

您查看控制台了吗?您可以看到“uncaughttypeerror:car.getPrice不是一个函数”。为什么您的setter被命名为getter?@Ramsharan是的,但函数在那里。对吗?你的代码中根本没有使用原型功能。你的代码中没有原型,你的setter就是你设定价格的地方
this.price=amount
您说您正在学习该教程,但您从未使用过第1步中的
prototype
。而且您没有在该代码中实现任何原型。我已经更新了,但输出没有变化。@学生,小提琴中的代码有很多问题。我敦促您阅读更多关于MDN上的功能范围和原型链的内容,意思是,虽然这里有一个工作提琴:,但我已经做了一些更改。提琴+3。我会尝试在相同的环境中使用
原型。
function carDetails() {
  var car = new vehicle("Red", "Car");
  car.getPrice(100);

  document.getElementById("color").value = car.color;
  document.getElementById("type").value = car.type;
  document.getElementById("price").value = car.price;

}

// Define a function which will work as a method
function getPrice(amount) {
  this.price = amount;
}


function vehicle(color, type) {
  this.color = color;
  this.type = type;
  this.getPrice = getPrice;
}

carDetails();

It will work
function carDetails() {
  var car = new vehicle("Red", "Car");
  car.getPrice(100);

  document.getElementById("color").value = car.color;
  document.getElementById("type").value = car.type;
  document.getElementById("price").value = car.price;

}

// Define a function which will work as a method
function getPrice(amount) {
  this.price = amount;
}


function vehicle(color, type) {
  this.color = color;
  this.type = type;
  this.getPrice = getPrice;
}

carDetails();

It will work