为什么函数原型不能在这个小提琴中工作?

时间:2015-12-27 17:26:34

标签: javascript html prototype prototype-programming function-prototypes

我试图使用javascript函数原型打印function carDetails() { var car = new vehicle("Red", "Car"); car.getPrice(100); // Define a function which will work as a method vehicle.prototype.price = function getPrice(amount) { car.price = amount; }; document.getElementById("color").innerHTML = car.color; document.getElementById("type").innerHTML = car.type; document.getElementById("price").innerHTML = car.price; } function vehicle(color, type) { this.color = color; this.type = type; //this.prototype.price = getPrice; } vehicle.prototype.getPrice = getPrice; carDetails();对象in my fiddle的值。



<h1>
Javascript function prototypes
</h1>
<p id='color'></p>

<p id='type'></p>

<p id='price'></p>
&#13;
vehicle
&#13;
&#13;
&#13;

为什么nested_attributes对象无法打印?

2 个答案:

答案 0 :(得分:2)

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

 car.getPrice(100);

该媒体资源为price,您使用getPrice()

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

答案 1 :(得分:1)

std::intializer_list<std::pair<const char, int>>