以下代码的作用如下:
WeatherWidget.prototype = new Widget;
Run Code Online (Sandbox Code Playgroud)
哪里Widget是构造函数,我想用新函数扩展Widget'类' WeatherWidget.
什么是new关键字在那里做什么以及如果被遗漏会发生什么?
请考虑以下代码.
function a() {}
function b() {}
function c() {}
b.prototype = new a();
c.prototype = new b();
console.log((new a()).constructor); //a()
console.log((new b()).constructor); //a()
console.log((new c()).constructor); //a()
Run Code Online (Sandbox Code Playgroud)
此外,请考虑以下内容.
console.log(new a() instanceof a); //true
console.log(new b() instanceof b); //true
console.log(new c() instanceof c); //true
Run Code Online (Sandbox Code Playgroud)
(new c()).constructor等于a()和Object.getPrototypeOf(new c())是a{ },怎么可能instanceof知道这new c()是一个实例c?我一直在阅读很多关于javascript中"继承"的文章.其中一些使用,new而其他人推荐Object.Create.我读的越多,我就越困惑,因为它似乎存在无穷无尽的变体来解决继承问题.
有人可以向我展示最受欢迎的方式(如果有的话,还是事实上的标准)?
(我想有一个基础物体Model,我可以延伸RestModel或LocalStorageModel).
我不明白javascript中继承的这种行为我总是看到它定义如下:
function GameObject(oImg, x, y) {
this.x = x;
this.y = y;
this.img = oImg;
this.hit = new Object();
this.hitBox.x = x;
this.hitBox.y = y;
this.hitBox.width = oImg.width;
this.hitBox.height = oImg.height;
}
Spaceship.prototype = new GameObject();
Spaceship.prototype.constructor = Spaceship;
function Spaceship(){
console.log("instantiate ship");
GameObject.apply(this, arguments);
this.vx = 0;
this.vy = 0;
this.speed = 3;
this.friction = 0.94;
}
Run Code Online (Sandbox Code Playgroud)
但就我而言,这些线路:
this.hitBox.width = oImg.width;
this.hitBox.height = oImg.height;
Run Code Online (Sandbox Code Playgroud)
当我在我的Spaceship构造函数中执行console.log(this)时,我可以看到proto属性设置为Spaceship而不是GameObject,如果我删除它们,则将其设置为GameObject.
如果我使用:
Spaceship.prototype = GameObject.prototype;
Run Code Online (Sandbox Code Playgroud)
我没有更多的问题.这阻止我的原因是我有另一个带有add()方法的对象,它使用以下代码检查GameObject的对象是否有效:
if(object instanceof GameObject)
Run Code Online (Sandbox Code Playgroud)
我不明白这两行可能会改变什么,以便继承在它们存在时被破坏而我不确定继承第二种方式是好的.有人可以告诉我这件事吗?:)
我正在学习面向对象的Javascript的一些方面.我遇到了这个片段
var Person = function(firstName, lastName)
{
this.lastName = lastName;
this.firstName = firstName;
};
Object.defineProperties(Person.prototype, {
sayHi: {
value: function() {
return "Hi my name is " + this.firstName;
}
},
fullName: {
get: function() {
return this.firstName + " " + this.lastName;
}
}
});
var Employee = function(firstName, lastName, position) {
Person.call(this, firstName, lastName);
this.position = position;
};
Employee.prototype = Object.create(Person.prototype);
var john = new Employee("John", "Doe", "Dev");
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么这个代码片段使用Object.create(Person.prototype)?我们不应该简单地重置原型:
Employee.prototype = Person.prototype;
Run Code Online (Sandbox Code Playgroud) 通过互联网搜索我总是碰到这种Javascript类扩展的方法
function extend(Child, Parent) {
var F = function() { }
F.prototype = Parent.prototype
Child.prototype = new F()
Child.prototype.constructor = Child
Child.superclass = Parent.prototype
}
Run Code Online (Sandbox Code Playgroud)
但是这与那个有什么不同呢?
function extend(Child, Parent) {
var p = new Parent()
Child.prototype = p
Child.prototype.constructor = Child
Child.superclass = p
}
Run Code Online (Sandbox Code Playgroud)
最后一个也很完美.那我为什么要用这个额外的var F = function() { }动作呢?