this的用法,比较不理解 希望各位能讲解的清晰详细点,让小弟学习一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this,始终指向到调用它的对象 <input type="text" onblue="alert(this.value);" />///////this 就是当前对象,同时可以这样,<script> var test=function(el){ alert(el.value);};</script><input type="text" onblue="alert(this);" /> 引用自http://blog.csdn.net/kiddy1985/archive/2009/04/03/4033153.aspxjava中的this随处可见,用法也多,现在整理有几点: 1. this是指当前对象自己。 当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中: public class Hello { String s = "Hello"; public Hello(String s) { System.out.println("s = " + s); System.out.println("1 -> this.s = " + this.s); this.s = s; System.out.println("2 -> this.s = " + this.s); } public static void main(String[] args) { Hello x="new" Hello("HelloWorld!"); } } 运行结果: s = HelloWorld! 1 -> this.s = Hello 2 -> this.s = HelloWorld! 在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果; 第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld! 2. 把this作为参数传递 当你要把自己作为参数传递给别的对象时,也可以用this。如: public class A { public A() { new B(this).print(); } public void print() { System.out.println("Hello from A!"); } } public class B { A a; public B(A a) { this.a = a; } public void print() { a.print(); System.out.println("Hello from B!"); } } 运行结果: Hello from A! Hello from B! 在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。 3. 注意匿名类和内部类中的中的this。 有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿嘀杏胻his时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面这个例子: public class A { int i = 1; public A() { Thread thread = new Thread() { public void run() { for(;;) { A.this.run(); try { sleep(1000); } catch(InterruptedException ie) { } } } }; //注意这里有; thread.start(); } public void run() { System.out.println("i = " + i); i++; } public static void main(String[] args) throws Exception { new A(); } } 在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。 4。在构造函数中,通过this可以调用同一class中别的构造函数,如 public class Flower{ Flower (int petals){} Flower(String ss){} Flower(int petals, Sting ss){ //petals++;调用另一个构造函数的语句必须在最起始的位置 this(petals); //this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数 } } 值得注意的是: 1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。 2:不能在构造函数以外的任何函数内调用构造函数。 3:在一个构造函数内只能调用一个构造函数。 必须用this关键字的三种情况: 1、我们想通过构造方法将外部传入的参数赋值给类的成员变量,构造方法的形式参数名称与类的成员变量名相同。例如: class Person { String name; public Person(String name) { this.name = name; } } 2、假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如: class Container { Component comp; public void addComponent() { comp = new Component(this); } } class Component { Container myContainer; public Component(Container c) { myContainer = c; } } 3、构造方法是在产生对象时被java系统自动调用的,我们不能再程序中像调用其他方法一样去调用构造方法。但我们可以在一个构造方法里调用其他重载的构造方法,不是用构造方法名,而是用this(参数列表)的形式,根据其中的参数列表,选择相应的构造方法。例如: public class Person { String name; int age; public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kiddy1985/archive/2009/04/03/4033153.aspx this指当前的宿主对象,默认指window对象var demo = {fn: function(){ alert(this.name);},name: 'this is demo'};demo.fn(); // 'this is demo' 今天cnblogs上有人才写的楼主看看:http://www.cnblogs.com/KevinYang/archive/2009/07/14/1522915.html 在HTML标签中this可以代表本标签。 this 指当前对象本身一般在当前对象的事件作为参数传到一个方法中,如楼上几个兄弟所示。在方法中对此参数进行的操作都是针对当前对象的。 <input type="text" onblue="alert(this.value);" />//this 当前对象<script> var test=function(obj){ alert(obj.valued+obj.name);};</script><input type="text" onclick="test(this)"/> this 指当前对象本身,当前的宿主对象,默认指window对象 就看 this 用在哪了 this还有就是调用其他构建器时必须放在第一句 想要一个邮箱验证的 正则表达式 可以验证 [email protected]这种的.... js代码在firefox和chrome中不执行,在IE中正常 为什么在ie下,上下左右没用呢? 浏览器除了window对象、document对象外还有哪些对象,哪里有帮助文档? 怎样获得iframe中链接中的html内容 局部刷新 JavaScript正则表达式求教 怎么判某个id是否存在???? 80高分等待 能否对ActiveXObject对象添加属性或方法?在线等待!!! style.color 奇怪问题 请教一个事件绑定的疑难问题 帮我看看这句话是什么意思啊?
///////this 就是当前对象,同时可以这样,
<script>
var test=function(el){
alert(el.value);
};
</script>
<input type="text" onblue="alert(this);" />
java中的this随处可见,用法也多,现在整理有几点: 1. this是指当前对象自己。 当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中: public class Hello { String s = "Hello"; public Hello(String s) { System.out.println("s = " + s); System.out.println("1 -> this.s = " + this.s); this.s = s; System.out.println("2 -> this.s = " + this.s); } public static void main(String[] args) { Hello x="new" Hello("HelloWorld!"); } } 运行结果: s = HelloWorld! 1 -> this.s = Hello 2 -> this.s = HelloWorld! 在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果; 第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld! 2. 把this作为参数传递 当你要把自己作为参数传递给别的对象时,也可以用this。如: public class A { public A() { new B(this).print(); } public void print() { System.out.println("Hello from A!"); } } public class B { A a; public B(A a) { this.a = a; } public void print() { a.print(); System.out.println("Hello from B!"); } } 运行结果: Hello from A! Hello from B! 在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。 3. 注意匿名类和内部类中的中的this。 有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿嘀杏胻his时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面这个例子: public class A { int i = 1; public A() { Thread thread = new Thread() { public void run() { for(;;) { A.this.run(); try { sleep(1000); } catch(InterruptedException ie) { } } } }; //注意这里有; thread.start(); } public void run() { System.out.println("i = " + i); i++; } public static void main(String[] args) throws Exception { new A(); } } 在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。 4。在构造函数中,通过this可以调用同一class中别的构造函数,如 public class Flower{ Flower (int petals){} Flower(String ss){} Flower(int petals, Sting ss){ //petals++;调用另一个构造函数的语句必须在最起始的位置 this(petals); //this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数 } } 值得注意的是: 1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。 2:不能在构造函数以外的任何函数内调用构造函数。 3:在一个构造函数内只能调用一个构造函数。 必须用this关键字的三种情况: 1、我们想通过构造方法将外部传入的参数赋值给类的成员变量,构造方法的形式参数名称与类的成员变量名相同。例如: class Person { String name; public Person(String name) { this.name = name; } } 2、假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如: class Container { Component comp; public void addComponent() { comp = new Component(this); } } class Component { Container myContainer; public Component(Container c) { myContainer = c; } } 3、构造方法是在产生对象时被java系统自动调用的,我们不能再程序中像调用其他方法一样去调用构造方法。但我们可以在一个构造方法里调用其他重载的构造方法,不是用构造方法名,而是用this(参数列表)的形式,根据其中的参数列表,选择相应的构造方法。例如: public class Person { String name; int age; public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kiddy1985/archive/2009/04/03/4033153.aspx
var demo = {
fn: function(){ alert(this.name);},
name: 'this is demo'
};
demo.fn(); // 'this is demo'
http://www.cnblogs.com/KevinYang/archive/2009/07/14/1522915.html
//this 当前对象
<script>
var test=function(obj){
alert(obj.valued+obj.name);
};
</script>
<input type="text" onclick="test(this)"/>