关于javaScript对象问题
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");为何会报错
求大神们指导。。。
相关推荐
【JavaScript源代码】详解JS对象遍历的顺序问题.docx
详解JS对象遍历的顺序问题 可能有些同学听过在 JavaScript 中遍历对象顺序不固定的这一说法。事实上,这个说法不是特别准确。 对待遍历顺序,对象有一套自己既定的规则,在此规则下呢,对象的遍历顺序会受插入元素顺序的影响,但是不完全受插入元素先后顺序的影响。如果您有「必须按插入元素顺序遍历」的场景,可以考虑使用 Map。 遍历对象的方法有很多种,我们经常会使用的有 for...in ,除此之外,还有: Object.keys Object.entries Obejct.getOwnerProPertyNames Reflect.ownKeys
javascript json字符串到json对象转义问题
今天小编就为大家分享一篇关于javascript json字符串到json对象转义问题,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
前台js对象在后台转化java对象的问题探讨
最近项目开发的过程中,多次遇到前台js对象在后台转化java对象的问题。记录下做法待用。 简单来说就是在前台用JSON.stringify()方法将js对象转化为js字符串,后台接收json字符串并将其转化为javaBean。 前台代码: 代码如下: var data = {}; data.id = $(‘#id’).val(); data.msg = $(‘#msg’).val(); //提交数据 $.post(contextPath + ‘/XXX.do?’+new Date().getTime(),{data: JSON.stringify(data)},function(result
js 对象外部访问或者调用问题
造成楼主的模糊的其实是this指向的问题,你可以用alert出this看看,他们分别指向的是什么,相信您就会明白了!在您写的那个c中的this其实指向的是c 而不是abc!
引入JS文件IE6报语法错误或缺少对象问题的解决方法
引入JS文件IE6报错或缺少对象问题想必很多朋友都有遇到过吧,这个问题在于文件编码上,下面为大家介绍下比较不错的解决方法


