Rebol真的具有javascript原型属性的等价物吗?

Reb*_*ial 5 javascript rebol

Gregogy在这里发了一篇关于rebol和javascript的帖子http://blog.revolucent.net/2009/05/javascript-rebol.html

但是当我更深入地比较javascript和rebol时,我无法看到javascript原型的rebol相当于什么.因为使用make in rebol从另一个扩展对象实例并不完全像javascript原型属性,因为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>
Run Code Online (Sandbox Code Playgroud)

更新:我当然不关心语法糖.只需重新定义一个对象,就无法阻止R2中的扩展.我的问题不是关于对象INSTANCE的扩展,而是关于所有INSTANCES的扩展:这就是js prototype属性所允许的.

所以重新提出我的问题:Rebol是否允许通过扩展像javascript这样的父类来自动扩展子类的所有实例,无论我不关心什么语法?

为了性能肯定我看到一个实例的R2和R3之间的区别,但至于语言功​​能我没有自动扩展所有子对象,这是一个很大的负担,因为我将不得不自己管理它们将是非常慢因为它不是由系统本身完成的.如果我想创建一个像jquery这样严重依赖这种功能的框架怎么办?这将是一个很大的麻烦.

Cyp*_*hre 7

Oldes是对的,默认情况下,REBOL中不存在类似JS的原型.但您可以自由创建适合您需求的功能.下面是使用嵌套上下文在多个实例之间共享值来模拟JS原型的简单示例:

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
Run Code Online (Sandbox Code Playgroud)


小智 6

REBOL没有等价物.

R3中的对象是使用任何其他对象作为原型创建的.但是,一旦创建,它就是一个独立的实体.对用作原型的对象的更改不会影响较新的对象 - 反之亦然.

REBOL 2中的对象一旦创建,就不能添加新的字段; 您所能做的就是基于旧对象创建一个新对象,但使用新字段.这可能很烦人,因为它可能会破坏对旧对象的引用.

REBOL 3在这方面要好得多.extendappend允许将新字段添加到任何对象.


这个脚本可能有点帮助:链接文本.

  • 它将目标对象与引用对象进行比较,并添加任何缺少的字段:
  • 它是REBOL 2代码,因此目标对象被副本替换而不是扩展
  • 但它确实通过任何嵌套对象进行递归,因此它可以在一次传递中进行复杂的嵌套更改