关于javaScript对象问题

lichencai 2015-10-22 11:49:27
// 定义一个类
function Set(){
this.values = {};
this.n = 0;
this.add.apply(this, arguments);
}

// 将方法add作为Set的原始方法
Set.prototype.add = function(){
for(var i = 0; i < arguments.length; i++){
var val = arguments[i];
var str = Set._v2s(val);
if(!this.values.hasOwnProperty(str)){
this.values[str] = val;
this.n++;
}
}
};
// 将值转成字符串
Set._v2s = function(val){
switch(val){
case undefined : return 'u';
case null : return 'n';
case true : return 't';
case false : return 'f';
default : switch(typeof val){
case 'number' : return '#' + val;
case 'string' : return '"' + val;
default : return '@' + objectId(val);
}
}

function objectId(o){
var prop = "|**objectid**|";
if(!o.hasOwnProperty(prop)) o[prop] = Set._v2s.next++;
return o[prop];
}
};
Set._v2s.next = 100; // 设置默认值
// 创建继承的方法
function inherit(p){
if(p == null){
throw TypeError();
}
if(Object.create){
return Object.create(p);
}
var t = typeof p;
if(t !== "object" && t !== "function"){
throw TypeError();
}
function f() {};
f.prototype = p;
return new f();
}
// 定义子类
function filteredSetSubclass(superclass, filter){
var constructor = function(){
superclass.apply(this, arguments);
};
var proto = constructor.propotype = inherit(superclass.prototype);
proto.constructor = constructor;
proto.add = function(){
for(var i = 0; i < arguments.length; i++){
var v = arguments[i];
if(!filter(v)) throw ("value " + v + " rejected by filter");
}
superclass.prototype.add.apply(this, arguments);
};
return constructor;
}

var StringSet = filteredSetSubclass(Set, function(x){return typeof x === "string";});
var stringSet = new StringSet();
// 执行这个方法的时候报错:Cannot read property 'apply' of undefined
stringSet.add("1");

本人最近在学习javaScript,上面是我学到的知识,不明白执行stringSet.add("1");为何会报错
求大神们指导。。。
...全文
187 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lichencai 2015-10-23
  • 打赏
  • 举报
回复
谢谢你,以后我会更加小心
forwardNow 2015-10-23
  • 打赏
  • 举报
回复

//   定义子类
function filteredSetSubclass(superclass, filter) {
    var constructor = function() {
        superclass.apply(this, arguments);
    };
    /*---------------------------------

    var proto = constructor.propotype = inherit(superclass.prototype);
    //  propotype => prototype 
      ---------------------------------*/
    var proto = constructor.prototype = inherit(superclass.prototype);

    proto.constructor = constructor;
    proto.add = function() {
        for (var i = 0; i < arguments.length; i++) {
            var v = arguments[i];
            if (!filter(v)) throw ("value " + v + " rejected by filter");
        }
        superclass.prototype.add.apply(this, arguments);
    };
    return constructor;
}
内容概要:本文围绕“光伏-储能-数据中心”系统的容量优化配置展开研究,旨在通过构建数学模型并结合Matlab编程实现,对园区内光伏发电、储能设备与数据中心算力负荷之间的多能互补关系进行协同优化。研究综合考虑可再生能源出力的波动性、储能系统的充放电特性以及数据中心的动态用电需求与算力调度特性,建立以最小化综合成本、降低碳排放强度、提升能源自给率等为目标的多目标优化模型,并采用先进的智能优化算法(如粒子群、灰狼、鲸鱼等)进行求解,从而确定光伏装机容量与储能系统配置的最优方案。文中提供了完整的Matlab代码实现流程,涵盖数据预处理、模型构建、算法求解与结果可视化全过程,属于综合能源系统与信息基础设施深度融合的前沿交叉课题。; 适合人群:具备电力系统、能源工程、自动化或计算机等相关专业背景,熟悉Matlab编程与基本优化算法原理,从事新能源利用、绿色数据中心、综合能源系统规划与低碳调度等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握光伏-储能-数据中心耦合系统的建模思路与多能流协同机制;② 学习基于Matlab的多目标容量优化配置方法与智能算法应用技巧;③ 为工业园区、数字经济园区及绿色数据中心的能源系统规划、节能减排与可持续运行提供科学决策依据和技术解决方案。; 阅读建议:建议结合所提供的Matlab代码,深入理解目标函数设计、约束条件设定及算法实现细节,可通过调整负荷参数、改变气候数据、引入新的优化目标或约束条件等方式进行拓展性实验,以深化对系统特性的认知并提升实际科研与工程应用能力。

87,988

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧