Gregogy 在这里发表了一篇关于 rebol 和 javascript 的文章 http://blog.revolucent.net/2009/05/javascript-rebol.html
但是随着我深入比较 javascript 和 rebol,我看不出什么是 javascript 原型(prototype)的 rebol 等价物。因为在 rebol 中使用 make 从另一个对象实例扩展对象实例并不完全像 javascript 原型(prototype)属性,因为 js 原型(prototype)允许一次扩展所有实例。
我是不是弄错了,或者是否有与下面的 rebol 代码等效的代码:
<html>
<head>
</head>
<body>
<script>
function Person(firstName, lastName, sex) {
this.firstName = firstName;
this.lastName = lastName;
this.whoAreYou = function() {
alert( "I've been built with Constructor and my name is " + this.firstName + " " + this.lastName);
}
this.WhatIsYourSex = function() {
alert(this.sex);
}
};
Person.prototype.sex = "Man";
</script>
<script>
JaneDoe = new Person("Jane", "Doe");
JaneDoe.whoAreYou();
JaneDoe.WhatIsYourSex();
alert("Are you sure?");
JaneDoe.sex = "Woman";
JaneDoe.WhatIsYourSex();
</script>
</body>
</html>
更新:我当然不关心语法糖。仅通过重新定义对象就可以阻止 R2 中的扩展。我的问题不是关于对象实例的扩展,而是关于一次扩展所有实例:这是 js 原型(prototype)属性允许的。
所以重新表述我的问题: Rebol 是否也可以通过扩展父类来自动扩展子类的所有实例,就像 javascript 一样,无论我不关心什么语法?
对于性能,我确实看到了 R2 和 R3 之间的区别,但对于语言功能特性,我没有自动扩展所有子对象,这是一个很大的负担,因为我必须自己管理它们非常慢,因为它不是由系统本身完成的。如果我想创建一个像 jquery 这样严重依赖这种功能的框架怎么办?这将是一个很大的麻烦。
最佳答案
Oldes 是对的,类 JS 原型(prototype)在默认情况下不存在于 REBOL 中。但是您可以自由创建适合您需要的功能。这是一个简单的例子,它使用嵌套上下文在多个实例之间共享值来模拟 JS 原型(prototype):
creature: func [
/prototype
field [word!]
value [any-type!]
/local result proto
][
proto: [
context [
static: context [
vars: reduce [
'kind "Monkey"
]
extend: func [
blk [block!]
field [word!]
value [any-type!]
/local pos
][
either pos: find/skip blk field 2 [
change/only next pos :value
][
insert/only insert tail blk reduce field :value
]
:value
]
get: func [
field [word!]
][
all [
field: any [
select/skip this/instance field 2
select/skip vars field 2
]
first field
]
]
set: func [
field [word!]
value [any-type!]
][
extend this/instance field :value
]
prototype: func [
field [word!]
value [any-type!]
][
extend vars field :value
]
who-are-you: does [
print ["Hello I'm" this/get 'kind this/get 'sex this/get 'first-name join this/get 'last-name "."]
]
]
instance: reduce [
'first-name none
'last-name none
]
;exported "API"
get: system/words/get in static 'get
set: system/words/get in static 'set
prototype: system/words/get in static 'prototype
who-are-you: system/words/get in static 'who-are-you
this: none
]
]
unless object? proto/1 [result: reduce proto append clear proto result]
if prototype [proto/1/prototype field :value]
result: make proto/1 []
result/this: result
]
creature/prototype 'sex "male"
jane: make creature []
jane/set 'first-name "Jane"
jane/set 'last-name "Rebol"
john: make creature []
john/set 'first-name "John"
john/set 'last-name "Doe"
jane/who-are-you
jane/set 'sex "female"
jane/who-are-you
john/who-are-you
creature/prototype 'kind "Human"
jane/who-are-you
john/who-are-you
关于javascript - Rebol 真的有 javascript 原型(prototype)属性的等价物吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4272018/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP
假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)
我想为我的Task模型创建一个status属性,该属性将按以下顺序指示它在三部分进度中的位置:打开=>进行中=>完成。它的工作方式类似于亚马逊包裹的交付方式:已订购=>已发货=>已交付。我想知道设置此属性的最佳方法是什么。我可能是错的,但创建三个独立的bool属性似乎有点多余。实现此目标的最佳方法是什么? 最佳答案 Rails4有一个内置的enummacro.它使用单个整数列并映射到键列表。classOrderenumstatus:[:ordered,:shipped,:delivered]end状态映射如下:{ordered:0,
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的