使用clojurescript从js原型对象中删除项目

时间:2014-03-24 02:25:12

标签: clojurescript

在js我做:

delete myobj.prototype["mything"]

如何用clojurescript完成这项工作?

2 个答案:

答案 0 :(得分:1)

只需使用内置的谷歌封闭包装删除:

(goog.object.remove (.-prototype myobj) "mything")

答案 1 :(得分:0)

我从来没有在生产模式下尝试过,但这个问题似乎是根据其他问题解决ClojureScript: How to add method via prototype to JS Object?

这是我提出解决问题的建议:

添加原型属性

(set! (.-foo (.-prototype js/String)) (fn [] "bar"))
(.-foo "test")
=>#<function (){return "bar";}>

取消此属性

(set! (.-foo (.-prototype js/String)) nil)
(.-foo "test")
=> nil
(undefined? (.-foo "test"))
=> false

关注&#34; undefined&#34; mozilla说明specification

undefined is a property of the global object, i.e. it is a variable in global scope. The initial value of undefined is the primitive value undefined.

然后我会尝试使用js / undefined

(set! (.-foo (.-prototype js/String)) js/undefined)
(.-foo "test")
=> nil
(undefined? (.-foo "test"))
=> true
祝你好运!