修改一下,生成html的那个函数忘了写id了,呵呵button.prototype.write=function(){
return "<button id=\"" + this.id + "\" style=\"width:" + this.width + ";height" + this.height + "\">" + this.caption + "</button>"
};
return "<button id=\"" + this.id + "\" style=\"width:" + this.width + ";height" + this.height + "\">" + this.caption + "</button>"
};
你可以看看js.net
也许下一版的浏览器就支持呢!
对于this._text,this._top这些写法我认为:在this这个对象中有_text和_top属性字段。
属性其实就是对象里的一个变量
function otest(){
this.name = "test"; //otest的name属性
}
2、如何创建函数对象的方法
方法其实就是对象里引用的一个函数
function otest(){
this.otherfunctio(); //otest的otherfunctio方法
}
3、如何创建函数对象的事件
对于事件没多大了解
4、this.xxx什么的我看别人写的都加了个_,比如this._text,this._top,是不是必须得加阿?
_text只是变量名即属性名,不一定要加_
http://www.i-see.net/bindows,那边的JavaScript代码是封装得比较好的,另外这段时间我何一个朋友合作写了《JavaScript的面向对象方式》,应该算比较全面的介绍JavaScript的OOP,不过不知道是在CSDN开发高手还是程序员中发表的,目前编辑还没有通知我
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script>
function person(name,gender,age,dog){
this.name=name;
this.gender=gender;
this.age=age;
this.dog=dog;
function person.prototype.setName(name){
this.name=name;
}
function person.prototype.getName(){
return this.name;
}
function person.prototype.setGender(gender){
this.gender=gender;
}
function person.prototype.getGender(){
return this.gender;
}
function person.prototype.setAge(age){
this.age=age;
}
function person.prototype.getAge(){
return this.age;
}
function person.prototype.speak(words){
alert(words);
}
function person.prototype.getDog(){
return this.dog;
}
}
function dog(name,color,age,barksound){
this.name=name;
this.color=color;
this.age=age;
this.barksound=barksound;
this.id=null;
this.emotion=null;
this.owner=null;
function dog.prototype.setId(id){
this.id=id;
}
function dog.prototype.getId(){
return this.id;
}
function dog.prototype.setName(name){
this.name=name;
}
function dog.prototype.getName(){
return this.name;
}
function dog.prototype.setColor(color){
this.color=color;
}
function dog.prototype.getColor(){
return this.color;
}
function dog.prototype.setAge(age){
this.age=age;
}
function dog.prototype.getAge(){
return this.age;
}
function dog.prototype.setBarkSound(barksound){
this.barksound=barksound;
}
function dog.prototype.getBarkSound(){
return this.barksound;
}
function dog.prototype.setEmotion(emotion){
this.emotion=emotion;
}
function dog.prototype.getEmotion(){
return this.emotion;
}
function dog.prototype.setOwner(owner){
this.owner=owner;
}
function dog.prototype.getOwner(){
return this.owner;
}
function dog.prototype.bark(){
alert(this.barksound);
}
function dog.prototype.callBark(){
this.bark();
}
}
function loadit(){
var dog1=new dog("Jeck","red",1,"wangwang");
myid.innerHTML="The dog's name is: "+dog1.getName();
dog1.setId(1);
dog1.setEmotion("happy");
myid.innerHTML+="<br>Its emotion is: "+dog1.getEmotion();
myid.innerHTML+="<br>Its id is: "+dog1.getId();
dog1.setOwner(new person("Alice",false,17,dog1));
myid.innerHTML+="<br>Its owner's name is: "+dog1.getOwner().getName();
dog1.getOwner().speak("Hello, my dog's name is "+dog1.getOwner().getDog().getName());
dog1.callBark();
}
</script>
</head><body onload="loadit()">
<p id="myid"></p>
<p id="myid2"></p>
</body></html>
function Person() {
this.userName="wsfx"; //属性一
this.password="123456"; //属性二
this.getPersonInfo=getPersonInfo; //定义对象的方法
}//注意,这个函数不能写在function A()的函数体内,不然会报错的。
function getPersonInfo() {
alert(this.userName);
alert(this.password);
}//下面是在使用Person对象的例子
<script language="javascript">
var p=new Person();
p.getPersonInfo();
</script>
//一起进步!
代码如下//调用
var ss=new myClass(); //实现function myClass(){
this.onchange=null;
this.setEvent();
}myClass.prototype.setEvent=function(){
alert("ss")
}运行到this.setEvent();
提示出错阿
看了你的提问,代码应该没有问题,prototype关键字是对于Object的parent object起作用的,而你的myClass这个函数是一个典型的constructor(构造子或者构造过程),用来产生一个myClass的实例,这点你可以通过instanceof这个操作来判断。至于你的代码出现错误,我想可能是你其他的问题吧,认真调试一下,或者贴出完整的源代码,我来帮你看看到底什么问题。
4、this.xxx什么的我看别人写的都加了个_,比如this._text,this._top,是不是必须得加阿?
this._text从Javascript来说同样是一个属性,就是说你可以确实调用的,不过作为WebLibrary的开发人员,最后的Document不会公布这个属性的,这个只是一个设计的规范问题罢了。this._text相当于载设计上认为只是内部方法自己调用的,所以通过_PropertyName这样的命名规范让别人好理解。一句话,不是必需的,只要通过this.xxx可以访问的,在你的instance中就绝对可以调用