Gregogy在http://blog.revolucent.net/2009/05/javascript-rebol.html上发布了有关rebol和javascript的帖子

但是随着我进一步比较javascript和rebol,我看不出javascript原型(prototype)的rebol等效于什么。因为在rebol中使用make in rebol从另一个对象实例扩展对象实例并不完全像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是对的,默认情况下,REBOL中不存在类似JS的原型(prototype)。但是您可以自由创建适合您需求的功能。这是一个简单的示例,该示例使用嵌套上下文在多个实例之间共享值以模拟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

09-25 18:16
查看更多