var dwProtector = new Class({
Implements: [Options],
options: {
image: 'blank.gif',
elements: $$('img'),
zIndex: 10
},
initialize: function(options) {
this.setOptions(options);
this.protect();
},
protect: function() {
this.options.elements.each(function(el) {
var size = el.getCoordinates();
var p = new Element('img', {
src: this.options.image,
width: size.width,
height: size.height,
styles: {
'z-index': this.options.zIndex,
'left': size.left + 'px',
'top': size.top + 'px',
'position': 'absolute'
}
}).inject($(document.body),'top');
},this);
}
});我这样使用,运行时却提示Class未定义,为什么呢?这种定义方法难道是错的?还是需要引入其他的JS文件?
var protector = new dwProtector({
image: '/dw-content/blank.gif',
elements: $$('.protect')
});
Implements: [Options],
options: {
image: 'blank.gif',
elements: $$('img'),
zIndex: 10
},
initialize: function(options) {
this.setOptions(options);
this.protect();
},
protect: function() {
this.options.elements.each(function(el) {
var size = el.getCoordinates();
var p = new Element('img', {
src: this.options.image,
width: size.width,
height: size.height,
styles: {
'z-index': this.options.zIndex,
'left': size.left + 'px',
'top': size.top + 'px',
'position': 'absolute'
}
}).inject($(document.body),'top');
},this);
}
});我这样使用,运行时却提示Class未定义,为什么呢?这种定义方法难道是错的?还是需要引入其他的JS文件?
var protector = new dwProtector({
image: '/dw-content/blank.gif',
elements: $$('.protect')
});
解决方案 »
- a href 和onclick的问题
- a标签 href提交表单: submit is not a function
- 如何在Text的Onfocus事件里创建一个DIV,并且位置正好在text的正下方左对齐?
- 用showmodaldialog如何不显示地址栏
- 关于下拉列表的一些问题
- 高分求一个用js实现的树型下拉列表框
- 求帮助-怎么实现精确的票据打印或者怎么实现只打印网业中指定的区域
- 急!!javaScript 添加下拉框数据,提交后,form中没有相应的数据,怎么回事
- 传数组,100分
- 打开文件夹的疑问
- q请问各位,在firefox中如何实现IE中这样的滤镜?
- jquery datepicker 之前日期怎么让它能选择,20分
image: '/dw-content/blank.gif',
elements: $$('.protect')
});上面的方法, new 一个 dwProtector 对象
dwProtector 得先定义...你是打算这样吗?
var protector = {
image: '/dw-content/blank.gif',
elements: $$('.protect')
};
(1)var dwProtector = new Class({
Implements: [Options],
.......
});
已经是一个匿名类的实例
(2)你想修改这个类的参数,看起来应该调用它的initialize()方法或者setOptions()方法
dwProtector.initialize({
image: '/dw-content/blank.gif',
elements: $$('.protect')
});
而不是从一个实例中new一个新的实例出来(这也是报告类未定义的原因)
不是,我只是想知道var dwProtector = new Class({这种JS类的定义方法是什么意思。
4楼wqkjj的回答,我好好理解下,确实是不大懂
===============================================================================
定义的含义:在LZ程序的某个地方,应该有如下类似的定义:
(1)取得某个对象的定义
Class = function( obj ) {
return obj;
}
或者。从var dwProtector = new Class({....});来看,应该是这种方式。
(2)生成对象的实例
Class = function( obj ) {
return new obj;
}
然后:
var dwProtector = new Class({....});其中{...}中的内容等价于obj。
这样定义的意义,要看你的原文才能确定,我猜如下:
(1)使JS的function来表达对象的方式转换成Class方式,因为大多数面向对象编程的程序员更习惯于Class。
(2)可能在Class的上有一些系统所有对象公共的属性和方法,需要通过Class类来实现类继承的模拟。
return obj;
}这对于js来说完全等于画蛇添足