var Class = function(properties){
//var _class=function(){
this.name='zhu';
this.say=function(){
alert('a')
}
//}
//return _class
}Class.empty = function(){};Class.prototype = {
say:function(){
alert('hi,world')
}, //这儿逗号
paly:function(){
alert('enenen')
}
}
var gg = new Class()for(var i in gg)
{alert(i);alert(gg[i]);}
alert('hi,world');
},
paly:function(){
alert('enenen');
}问题1,少了一个 ,
问题2: new Class()返回的是一个function,当构造函数当返回的是一个对象时,会用此对象代替new 出来的对象作为返回值你alert(gg)就知道了...
//第一个函数主要是想利用js闭包功能,后面需要加()
var Class = function(properties){
var _class=function(){
this.name='zhu';
this.say=function(){
alert('a')
}
}
return _class
}()
//第2个就是追加一个empty没有参数的函数
Class.empty = function(){};
//第3个就是功能的扩展,类似功能追加。
Class.prototype = {
say:function(){
alert('hi,world')
},
//这个地方逗号是必须的!
paly:function(){
alert('enenen')
}
}
//这些就是对象的声明和调用了
var gg = new Class()
//gg.say();
//gg.paly();
for(var i in gg) {alert(i);alert(gg[i]);}//你可以再试一下,这段代码主要是js面向对象编程!
<script>
var Class = function(properties){
var _class=function(){
this.name='aa'
}
_class.prototype = properties
return _class
}var c = new Class({
say:function(){
alert('hi,world')
},
paly:function(){
alert('enenen')
}
})var d = new Class(
{
goo:function(){
alert('goo')
},
too:function(){
alert('too')
}
})
var gg = new c()
var ggg = new d()
alert(gg.name)
gg.say();
gg.paly();
ggg.goo()
ggg.too()
</script>
顺便请教一下各位大虾,这好象是先创建了一个构造函数,在根据构造函数去创建类的。这中写法叫什么啊??
还有就是 在我看的代码中
有一个
Class.prototype = {
xx : function(properties)
{
alert('xx')
}
}
我用ggg.xx(),似乎并不起作用,但是代码中却又有,确实不知道应该怎么克用它
各位大虾,指教下吧。。
var Class = function(properties){
var _class=function(){
this.name='aa'
}
_class.prototype = properties
return _class
}
在我看来,写这样子一个类不外忽两个作用:
1.为传进来的对象增加默认的方法或是属性,这里就是this.name
2.创建一份对象不可修改的拷贝,这个是通过prototype 实现的,因为对象只能覆盖prototype 里面的属性或是方法,但是无法修改里面的属性和方法
gg.paly();会执行alert的啊!
xx : function(properties)
{
alert('xx')
}
}
继承