JS中的每个函数构造函数都有一个prototype.constructor属性。它存储函数的定义: function Rabbit(value) {
this.jumps: value;
}
alert(Rabbit.prototype.constructor); // alerts exactly the definition of the Rabbit function
现在
这些功能是如何运作的?第一个更典型的是我在考虑构造函数时考虑的内容。 示例1:使用这个来命名和设置属性。然后使用new创建一个新的Book对象。 function Book(name, numPages) {
this.name = name;
this.numPages = numPages;
}
var myBook = new Book('A G
我通过CodeAcademy JS excercises工作,有一个关于这个例子的问题: //Animal class
function Animal(name) {
this.name = name;
}
//Attach sayName method to Animal class
Animal.prototype.sayName = function() {
c
我有这样的例子。 function Bar() {
this.barVal = "BarValue";
}
function Foo() {
this.fooVal = "FooValue";
Bar.apply(this, arguments); // here I use apply() to get access everything in Bar fro