pr1*_*001 17 javascript inheritance constructor prototype
这是我想做的事情:
function a() {
// ...
}
function b() {
// Some magic, return a new object.
}
var c = b();
c instanceof b // -> true
c instanceof a // -> true
b instanceof a // -> true
Run Code Online (Sandbox Code Playgroud)
可能吗?我可以通过挂钩到它的原型链b
来成为一个a
容易的实例a
但是我必须这样做new b()
,这就是我想要避免的.我想要的是什么?
更新:我觉得有可能明智地使用b.__proto__ = a.prototype
.我下班后要去做更多的实验.
更新2:下面是你可以得到的最接近的,这对我来说已经足够了.谢谢所有有趣的答案.
function a() {
// ...
}
function b() {
if (!(this instanceof arguments.callee)) {
return new arguments.callee();
}
}
b.__proto__ = a.prototype
var c = b();
c instanceof b // -> true
c instanceof a // -> false
b instanceof a // -> true
Run Code Online (Sandbox Code Playgroud)
更新3:一旦我添加了必要的一行,我就在'power constructors'的博客文章中找到了我想要的内容b.__proto__ = a.prototype
:
var object = (function() {
function F() {}
return function(o) {
F.prototype = o;
return new F();
};
})();
function a(proto) {
var p = object(proto || a.prototype);
return p;
}
function b(proto) {
var g = object(a(proto || b.prototype));
return g;
}
b.prototype = object(a.prototype);
b.__proto__ = a.prototype;
var c = b();
c instanceof b // -> true
c instanceof a // -> true
b instanceof a // -> true
a() instanceof a // -> true
Run Code Online (Sandbox Code Playgroud)
Koo*_*Inc 29
您可以使用此模式:
function SomeConstructor(){
if (!(this instanceof SomeConstructor)){
return new SomeConstructor();
}
//the constructor properties and methods here
}
Run Code Online (Sandbox Code Playgroud)
之后你可以这样做:
var myObj = SomeConstructor();
Run Code Online (Sandbox Code Playgroud)
除了这个(相当古老的)答案:您可以使用模块模式来创建对象:
function Person(name, age, male) {
name = name || 'unknown';
age = age || 0;
function get() {
return ['This person is called ', name,
(!male ? ', her' : ', his'),' age is ',
age].join('');
}
function setAge(nwage) {
age = nwage;
}
return Object.freeze({get: get, setAge: setAge});
}
// usage
var jane = Person('Jane', 23)
,charles = Person('Charles', 32, 1)
,mary = Person('Mary', 16);
console.log(jane.get()); //=> This person is called Jane, her age is 23
mary.setAge(17);
console.log(mary.get()); //=> This person is called Mary, her age is 17
Run Code Online (Sandbox Code Playgroud)
这是我使用该模式创建的一些函数的jsFiddleDate
.
Mat*_*all 10
使用new
关键字有什么问题?
无论如何,听起来最好的事情是阅读Javascript继承:http: //javascript.crockford.com/inheritance.html
是的。如果您不想使用“new”关键字,只需使用原型,并在构造函数中返回一些内容。
使用“new”只是告诉构造函数:
构造函数中的“this”关键字应该引用函数本身,而不是父对象(像往常一样),如果该函数在全局范围内声明,则父对象将是窗口对象。
对于新创建的对象/实例上所有失败的查找(未找到 obj 属性),请检查原始构造函数的原型属性。
所以有了新的:
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.getDistance = function(otherPoint){
var Dx = (this.x - otherPoint.x) ** 2;
var Dy = (this.y - otherPoint.y) ** 2;
var d = Dx + Dy;
d = Math.sqrt(d);
return d
}
var pointA = new Point(3, 6);
var pointB = new Point(5, 8);
var distanceAB = pointA.getDistance(pointB);
Run Code Online (Sandbox Code Playgroud)
没有新的:
function Point(x, y) {
let d = Object.create(Point.prototype);
d.x = x;
d.y = y;
return d
}
Point.prototype.getDistance = function(otherPoint){
var Dx = (this.x - otherPoint.x) ** 2;
var Dy = (this.y - otherPoint.y) ** 2;
var d = Dx + Dy;
d = Math.sqrt(d);
return d
}
var pointA = Point(3, 6);
var pointB = Point(5, 8);
var distanceAB = pointA.getDistance(pointB);
Run Code Online (Sandbox Code Playgroud)
尝试添加删除第一个示例中的“new”,您将看到“this”不再引用构造函数,而是引用窗口对象。您将提供窗口 x 和 y 属性,这可能不是您想要的。
归档时间: |
|
查看次数: |
24162 次 |
最近记录: |