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')
});

解决方案 »

  1.   

    应该是没有引用prototype.js
      

  2.   

    dwProtector 相对于的这个库文件,楼主没有引用,不知道楼主用到了什么.js,在使用之前引用了就可以了
      

  3.   

    var protector = new dwProtector({
        image: '/dw-content/blank.gif',
        elements: $$('.protect')
    });上面的方法, new 一个 dwProtector 对象
    dwProtector 得先定义...你是打算这样吗?
    var protector = {
        image: '/dw-content/blank.gif',
        elements: $$('.protect')
    };
      

  4.   

    LZ有些概念没有整明白:
    (1)var dwProtector = new Class({
        Implements: [Options],
        .......
       });
      已经是一个匿名类的实例
    (2)你想修改这个类的参数,看起来应该调用它的initialize()方法或者setOptions()方法
       dwProtector.initialize({
        image: '/dw-content/blank.gif',
        elements: $$('.protect')
       });
       而不是从一个实例中new一个新的实例出来(这也是报告类未定义的原因)
      

  5.   


    不是,我只是想知道var dwProtector = new Class({这种JS类的定义方法是什么意思。
    4楼wqkjj的回答,我好好理解下,确实是不大懂
      

  6.   

    对啊,你的Class应该是一个类,应该在某个地方定义了的
      

  7.   

    js没有new Class这种申明方式
      

  8.   

    回过头再看了看问题,对于"Class未定义的错误",还是前面lieri111(月革雨)  #2楼的答复是对的。
    ===============================================================================
    定义的含义:在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类来实现类继承的模拟。
      

  9.   

    Class = function( obj ) {
      return obj;
    }这对于js来说完全等于画蛇添足