var Point = function() {
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { x = px; y = py; };
this.showPoint = function() { alert("x=\t" + x + "\ny=\t" + y); };
};
var ColorPoint = function(){ this.color = "#FFFFFF";
Point.call(this); //this 指的是Obj
};
var p1= new ColorPoint();
p1.setPoint(5,5);
p1.showPoint(); //这里是 "x = 5 y = 5"
alert(p1.x); //这里是 1 //没想到啊!
//气死我了,没想到啊!
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { x = px; y = py; };
this.showPoint = function() { alert("x=\t" + x + "\ny=\t" + y); };
};
var ColorPoint = function(){ this.color = "#FFFFFF";
Point.call(this); //this 指的是Obj
};
var p1= new ColorPoint();
p1.setPoint(5,5);
p1.showPoint(); //这里是 "x = 5 y = 5"
alert(p1.x); //这里是 1 //没想到啊!
//气死我了,没想到啊!
解决方案 »
- 延迟加载图片 Lazy Load
- img src 的问题
- 关于extjs的菜人问题
- javascript 怎样调用IE文件中的打印预览
- http://glume.17113sf.cn
- Netscape下,如何赋给控件click事件?
- 初学,求教,关于将页面table中的数据导出到excel中,以及将excel中的数据导入到页面中,急!!!
- 如何由form object获得其owner document
- 在可编辑的iframe区域中怎样在"大层"的右侧从上到下并排放置"小层?不想用表格定位
- 在a.htm页面中,有一个<asp:button runat=server>按钮,点击的弹出一个b.htm页,如何在b.htm关闭时,刷新a.htm呢。
- jQuery1.6中文版API CHM版的。排版遵循了1.4的友好型排版。赶快下载啦
- JavaScript直接调用后台的WCF服务进行数据的增删改查Demo 要的赶紧下载
this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
在最后加一行,果然是这个样子
alert("window.x==="+window.x+";window.y==="+window.y);//windowx===5;window.y===5
你说,我要初始化,可是怎么个初法?
Point.call(this); //this 指的是Obj
这一行,js 究竟干了什么?是这样吗?下面的 两种等效吗?
var Point = function() {
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { this.x = px; this.y = py; };
this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
};
var ColorPoint = function(){ this.color = "#FFFFFF";
Point.call(this); //this 指的是Obj
};
var ColorPoint = function(){
this.color = "#FFFFFF";
//Point.call(this); //这删了这行,加了下面那一句.
(function() {
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { this.x = px; this.y = py; };
this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
})();
};
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { this.x = px; this.y = py; };
this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
}这部分其实是一个构造函数,只能用new构造对象,而不能直接执行。其实call就是让一方享有另一方的数据和方法,所以一定要说等效的话就是下面这样子:var ColorPoint = function(){ this.color = "#FFFFFF";
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { this.x = px; this.y = py; };
this.showPoint = function() { alert("x=\t" + this.x + "\ny=\t" + this.y); };
};
错在this.x和x的理解。var Point = function() {
this. x = 1;
this. y = 1;
this.setPoint = function(px, py) { x = px; y = py; };//这个x,y是在全局变量中定义的
this.showPoint = function() { alert("x=\t" + x + "\ny=\t" + y); };
};Point.call(this); 这句其实就是继承Point了,就是在var ColorPoint = function(){}后加上ColorPoint.prototype = new Point();这句this.x和x的区别还是存在的.
Point.call(this)
意思就是
执行Point函数 改变Point函数的this指向(默认是window)
现在已把 《javaScript 王者归来》 看完了。
似懂非懂的,
想问问 下面的继承的call的返回值是什么?
function Class3()
{
this.alertText= function(txt)
{
alert(txt);
}
}
function Class5()
{
this.showText= function(txt)
{
document.write(txt);
}
}
function Class4()
{
Class3.call(this); //本身继承class3 //这个call 方法,它会返回什么给我?
Class5.call(this); //本身继承class5//这个call 方法,它会返回什么给我?
}
var cl4 = new Class4();
cl4.showText("哈哈"); //
cl4.alertText("哈哈"); // ///谁能 详细解释一下啊.
这个call 的内部是怎么实现的? 我们可不可以重写 call 函数啊?---是想看看call 里面的代码!
没有实例化的时候 都是指向window的 并不是“构造函数本身的this都是指向对象本身”“call不会改变Point的this指向” 我说的改变只是形象的说一下 可以说是伪造
this.data =d?d:"字";
};
var Vocable = function(d){
this.datas =d?d:"词语";
Word.call(this,"A");//<1>这句,请看我在下面的解释。
};
var ve = new ("新年快乐!!"); //<2>
我的解释: //请你说说下面的理解对不对!
<1>:这句话的意思是:
调用 函数Word 但在 函数Word内部的this 这个针指,让它指向Vocable 所实例化的对象,
(如果,Vocable 的构造函数是 语句<2> 引发的,那么 Word内部的this针指 指向语句<2>的变量 ve )
this.a = "123"
}function b(){
a.call(this) //执行a,伪造a的this指向当前对象
//以下就可以访问a内部的东西了}
alert((new b()).a) //123
还有一个问题,大家 都把 call apply 这类继承叫做“构造继承”。
听说 构造继承 比 复制继承 快,
这个,不太明的。
按我的理解是:几乎一样快,因为他们一个编历一遍父类,一个执行一次父类, 都是一遍啊!
复制继承 :(下面是我自已写的,也不知对不对。)
var Word = function (d){
this.data =d?d:"字";
};
var Vocable = function(d){
this.datas =d?d:"词语";
for(var key in Word )
var ve = new Vocable("新年快乐!!"); //<2>
{
this[key] = Word[key];
}
};
var Word = function (d){
this.data =d?d:"字";
};
var Vocable = function(d){
this.datas =d?d:"词语";
for(var key in Word )
{
this[key] = Word[key];
}
};
var ve = new Vocable("新年快乐!!"); //<2>
Point.call(this); //this 指的是Obj
};你的这个其实就是定义一个类,类似java中的类。里面的this指向当前的对象,而不是window。其实你的这段代码如果这样写会更清楚一些
function ColorPoint (){ this.color = "#FFFFFF";
Point.call(this); //this 指的是Obj
};这两个是一段等同的代码。