我试图使用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;
为什么nested_attributes
对象无法打印?
答案 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>>