function Rectangle(w,h) {
this.width = w;
this.height = h;
this.long = 10;
}
Rectangle.prototype.volume = 20;
Rectangle.prototype.area = function() {return this.width * this.height;};
Rectangle.prototype.area2 = function() {return this.long * this.volume;};
function PositionedRectangle(x, y, w, h) {
Rectangle.call(this, w, h);
this.x = x;
this.y = y;
this.long = 100;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
delete PositionedRectangle.prototype.long;
delete PositionedRectangle.prototype.volume;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}var r2 = new PositionedRectangle(2,2,2,2);
document.write(r2.contains(3,3) + "<br />");
document.write(r2.height+ "<br />");
document.write(r2.area()+ "<br />");
document.write(r2.area2()+ "<br />");
document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);delete没有做到删除超类 Rectangle的任何属性,后面r2.heigh,r2.area(),r2.area2()…… 都有结果?求助delete到底做了什么?为什么没有达到删除属性的效果
this.width = w;
this.height = h;
this.long = 10;
}
Rectangle.prototype.volume = 20;
Rectangle.prototype.area = function() {return this.width * this.height;};
Rectangle.prototype.area2 = function() {return this.long * this.volume;};
function PositionedRectangle(x, y, w, h) {
Rectangle.call(this, w, h);
this.x = x;
this.y = y;
this.long = 100;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
delete PositionedRectangle.prototype.long;
delete PositionedRectangle.prototype.volume;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}var r2 = new PositionedRectangle(2,2,2,2);
document.write(r2.contains(3,3) + "<br />");
document.write(r2.height+ "<br />");
document.write(r2.area()+ "<br />");
document.write(r2.area2()+ "<br />");
document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);delete没有做到删除超类 Rectangle的任何属性,后面r2.heigh,r2.area(),r2.area2()…… 都有结果?求助delete到底做了什么?为什么没有达到删除属性的效果
解决方案 »
- 用js怎样隐藏地址栏和菜单栏
- 环境:在模板也中使用Jquery出现小问题大虾们,看看这问题出在哪?
- 实现鼠标拖动div 后交换位置如何实现?
- 没分了,江湖救急!! 如何将 "abc#123\#456" split成数组[abc, 123\#456]
- ---------------超难问题,请高手帮忙--------------
- 如果在一个父页面中用iframe调用了一个子页面,如何才能在子页面中控制父页面中的表单控件,比如id=text1的文本框的内容
- 有没有能对文本框中的数字进行加减的控件???
- 再网页的任何一个空位置双击就自动慢慢地滚屏,一直滚到最底下如何写?
- 输入行号和列号,点击动态生成的table,怎么防止点击按钮的时候table一直累加
- [前端-chrome]请问怎么通过console面板对页面进行一些交互
- javascript中的小问题?
- 请高手帮我看看哪错了
function t()
{
}
t.prototype.f=function(){};
var o=new t();
alert(o.f);
delete o.constructor.prototype.f;
alert(o.f);
</script>现在要考虑的是delete t.prototype 反而是不可以的 实例的o.f调用的是 o.constructor.prototype.f 而不是 t.prototype楼主的疑惑应该可以部分解答了,这个还是跟原型链的概念有关
function Rectangle(w,h) {
this.width = w;
this.height = h;
this.long = 10;
}
Rectangle.prototype.volume = 20;
Rectangle.prototype.area = function() {return this.width * this.height;};
Rectangle.prototype.area2 = function() {return this.long * this.volume;};
function PositionedRectangle(x, y, w, h) {
Rectangle.call(this, w, h);
this.x = x;
this.y = y;
this.long = 100;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
delete PositionedRectangle.prototype.long;
delete PositionedRectangle.prototype.constructor.prototype.volume;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}var r2 = new PositionedRectangle(2,2,2,2);
document.write(r2.contains(3,3) + "<br />");
document.write(r2.height+ "<br />");
document.write(r2.area()+ "<br />");
document.write(r2.area2()+ "<br />");
document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);alert(r2.volume);</script>实验结果验证了我的想法
this.width = w;
this.height = h;
this.long = 10;
呢?这些代码引用 JavaScript权威指南 第五版
function Rectangle(w,h) {
this.width = w;
this.height = h;
this.long = 10;
}
Rectangle.prototype.volume = 20;
Rectangle.prototype.area = function() {return this.width * this.height;};
Rectangle.prototype.area2 = function() {return this.long * this.volume;};
function PositionedRectangle(x, y, w, h) {
// Rectangle.call(this, w, h);
this.x = x;
this.y = y;
this.long = 100;
}
PositionedRectangle.prototype = new Rectangle();
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
delete PositionedRectangle.prototype.long;
delete PositionedRectangle.prototype.constructor.prototype.width;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}var r2 = new PositionedRectangle(2,2,2,2);
document.write(r2.contains(3,3) + "<br />");
document.write(r2.height+ "<br />");
document.write(r2.area()+ "<br />");
document.write(r2.area2()+ "<br />");
document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);alert(r2.width);</script>
还有,根据这段代码的例子,如果你删除了 Rectangle.call(this, w, h);这一步,width,height属性无法被子类函数调用,最后显示的结果就是undefined了,,,这看起来好矛盾,难道JavaScript权威指南例子就是存在这个矛盾的问题……
this.width = w;
this.height = h;
this.long =10;
}
Rectangle.prototype.volume = 20;
Rectangle.prototype.area = function() {return this.width * this.height;};
Rectangle.prototype.area2 = function() {return this.long * this.volume;};
function PositionedRectangle(x, y, w, h) {
Rectangle.call(this, w, h);
this.x = x;
this.y = y;
this.long = 100;
}
PositionedRectangle.prototype = new Rectangle();
PositionedRectangle.prototype.volume = 200;
//PositionedRectangle.prototype.constructor = PositionedRectangle;
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
delete PositionedRectangle.prototype.volume;
delete PositionedRectangle.prototype.long;
delete PositionedRectangle.long;
delete PositionedRectangle.prototype.constructor.prototype.width;
delete PositionedRectangle.prototype.constructor.prototype.volume;
delete PositionedRectangle.prototype.constructor.prototype.long;
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}var r2 = new PositionedRectangle(2,2,2,2);
document.write(Rectangle.prototype);//toString()返回
document.write(r2.contains(3,3) + "<br />");
document.write("width:" + r2.width+ "<br />" + "volume:" + r2.volume+ "<br />" + "long:" + r2.long );
document.write(r2.area()+ "<br />");
document.write("long*volume:" + r2.area2()+ "<br />");
document.write(r2.x + "," + r2.y + "," + r2.width + "," + r2.height + "<br />");
document.write(r2 instanceof PositionedRectangle && r2 instanceof Rectangle && r2 instanceof Object);最终这样测试了,Rectangle.call(this, w, h);这一步的参数w,h调用暂时不去管,上面测试结果感觉构造函数里this.Attribute 在构造函数里创建的属性delete无法删除
delete PositionedRectangle.prototype.xxx 删除的是PositionedRectangle.prototype原型创建的属性
delete PositionedRectangle.prototype.constructor.prototype.xxx 删除的是Rectangle原型Rectangle.prototype创建的属性
按照权威指南里源代码注释说:
// We create this prototype object for inheritance purposes, but we
// don't actually want to inherit the width and height properties that
// each Rectangle object has, so delete them from the prototype.
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;感觉并没有起到删除从Rectangle继承的属性,不理解注释的意义,PositionedRectangle.prototype里也没有width和height