What is the difference between Ruby open class and JavaScript prototype object? -


in ruby can use openclass add properties this:

class string    attr_accessor :ruby    def open       ruby = 'open'    end    def close       ruby = 'close'    end end 

then can use

x = string.new x.open # set ruby 'open' 'open' x.close # set ruby 'close' 'close' 

in javascript can use prototype add properties this:

string.prototype.ruby = {} string.prototype.open = function () { this.ruby = "open"; } string.prototype.close = function () { this.ruby = "close"; } 

then can invoke these prototype methods this:

var x = new string() console.log(['ruby', x.ruby].join(':')) /* when invoke open */ x.open() console.log(['ruby', x.ruby].join(':')) /* when invoke close */ x.close() console.log(['ruby', x.ruby].join(':')) 

are same thing?


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -