如何正确扩展Obect.prototype? [英] How to extend Obect.prototype correctly?
问题描述
我正在编写一个JavaScript库,它提供了函数 tablify(任何东西);
,它能够将任何数组或对象表示为HTML表。
I'm writing a JavaScript Library that offers the function tablify(anything);
, which is able to represent any Array or Object as an HTML Table.
现在我正在尝试扩展Array和Object原型,以便像这样使用它:
Now I'm trying to extend the Array and Object prototypes in order to use it like this:
var arr = ["a", "b", "c"];
var obj = {"A": "a", "B": "b", "C": "c"};
arr.tablify();
obj.tablify();
这是我的方法:
Array.prototype.tablify = function() {
return tablify(this);
}
Object.defineProperty(Object.prototype, 'tablify', {
value: function () {
return tablify(this);
},
writable: true,
configurable: true,
enumerable: false
});
问题是,JavaScript中的所有内容都是对象,因此不仅仅是<$ c $这样的文字c> {a:1,b:2} 可以转换,但也可以转换其他所有内容。
The issue is, that everything in JavaScript is an Object and therefore not only literals like {a: 1, b: 2}
can be converted, but also everything else.
这不会那么大处理,因为我的 tablify()
也可以处理原始类型,但当我扩展Object.prototype时,输入任何东西
总是返回object
,我再也无法区分类型了:
This wouldn't be such a big deal since my tablify()
can also deal with primitive types, but when I extend Object.prototype, typeof anything
always returns "object"
and I can't distinguish between types anymore:
function tablify(object) {
if (object instanceof Array) {
return ArrayToTable(object);
}
//This is always true if I extend Object.prototype:
if (typeof object === "object") { //only for "normal" objects like "{a: 1, b: 2, c: [],...}"
return ObjectToTable(object);
}
return PrimitiveToTable(object); //strings, numbers, functions, ...
}
- 为什么
typeof
总是返回object
? - 是吗? JS-API提供这种功能是一件好事(用.tablify();扩展数组/对象?)?
- 如何区分正常对象,数字,函数,......?
- 是否可以只扩展普通对象?
(禁止(42).tablify();
,string.tablify();
。 ..) - 这些普通物品的名称是什么?我该怎么称呼他们?
- Why is
typeof
always returning"object"
? - Is it a good thing for a JS-API to provide this sort of functionality (extending Arrays/Objects with ".tablify();")?
- How is it possible to distinguish between "normal" objects, numbers, functions,... ?
- Is it possible to only extend "normal" objects?
(forbid
(42).tablify();
,"string".tablify();
...) - Whats the name for those "normal" objects? How should I call them?
推荐答案
为什么
typeof
总是返回object
?
因为在草率模式下, 此
上下文始终应该是一个对象。当您调用函数或传递undefined或null时,您将获得全局对象,当您调用方法时,上下文将被转换为对象。
Because in sloppy mode, the this
context always is supposed to be an object. When you call a function or pass undefined or null, you get the global object, when you call a method the context will get casted to an object.
使用 tablify <= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode\"rel =nofollow noreferrer>严格模式 code>方法它会起作用!
Use strict mode for your tablify
method and it will work!
JS-API提供这种功能是件好事吗(使用
.tablify();
)扩展数组/对象?
是。 没有。。当您想要共享脚本时,许多人会皱眉使用您的脚本。有关详细讨论,请参阅为什么扩展本机对象是一种不好的做法?。
Yes. No.. Many people will frown to use your script when you want to share it. See Why is extending native objects a bad practice? for a detailed discussion.
至少你正确使用了不可枚举的属性 - 但我建议你在<$上使用它们c $ c> Array.prototype 同样,有太多人在枚举中滥用。
At least you've properly used non-enumerable properties - but I would recommend to use them on Array.prototype
as well, too many people abuse for in
enumerations.
如何区分普通对象,数字,函数......?
How is it possible to distinguish between "normal" objects, numbers, functions,... ?
typeof
似乎没问题。
是否可以只扩展正常 物体? (禁止(42).tablify();,string.tablify(); ...)
Is it possible to only extend "normal" objects? (forbid (42).tablify();, "string".tablify(); ...)
不是。所有原始包装器都继承自 Object.prototype
。
Not really. All primitive wrappers inherit from Object.prototype
.
这些名称是什么正常的物体?我该怎么称呼他们?
Whats the name for those "normal" objects? How should I call them?
只是对象。或者如果你想要普通对象(将它们与数组,内置函数,宿主对象区分开来)。
Just "objects". Or "plain objects" if you want (distinguishes them from arrays, built-ins, host objects).
这篇关于如何正确扩展Obect.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!