Ruby and JavaScript are both dynamic scripting languages, and many of them are similar. For example, all functions are first-class objects and all have high-level functions... However, I think the most important thing is to belong to their object model. For example, they are of a nondescribable type and the object must be like an attribute slot. Ruby's object model Object-Oriented Programming scripting language. The biggest feature of Ruby is its object-oriented model and metadata programming capabilities. Ruby is completely object-oriented and does not have any primitive types. It should be noted that Ruby is an orthodox object-oriented programming language, and JavaScript is not. In a word, the Ruby object model is a class, even if the class is an object, and the object is also a Hash. How can this problem be solved? First take a look at the class is Class
The orthodox syntax for creating classes and objects can be directly translated into Java code. However, Ruby classes exist not only in static source files, but also in runtime. Because a Ruby Class is an Object of the Class, it is also a subclass of the Object. Great! View codeC = Class.new D = Class.new(C) {include M def the_method"general method"end}
Here we can not only use the method of creating objects to generate classes, but also implement inheritance and definition of Class bodies. It is even more surprising that because the Class is an object of the Class, the Class method is the Class instance method. The Class inherits the Module, the Module inherits the Object, and the Object is mixed into the Kernel Module (the Object is also a Class, chicken or egg ?). Therefore, their instance methods are class methods (also called Singleton methods), equivalent to static Java methods. Next, let's take a look at the object as an attribute slot.o = Object.newo.instance_variable_set(:@x, 0) # Note required @ prefixo.instance_variable_get(:@x) # => 0o.instance_variable_defined?(:@x) # => trueObject.class_variable_set(:@@x, 1) # Private in Ruby 1.8Object.class_variable_get(:@@x) # Private in Ruby 1.8Object.class_variable_defined?(:@@x) # => true; Ruby
In addition to the redundant syntax, we can see that an object is facing external torture like a hash table. Next, add an attribute to the hash table.class ClassMethodExample def self.foo puts 'class foo' end class << self def bar puts 'class bar' end endendclass << ClassMethodExample def baz puts 'class baz' endenddef ClassMethodExample.tor puts 'class tor'endClassMethodExample.fooClassMethodExample.barClassMethodExample.bazClassMethodExample.tor
This is the syntax for adding class methods to a class (it tells you quietly that class methods can be inherited). For pure objects, you can also define the instance methods of objects.Class Person def talk puts "This is a Person class. "endendp1 = Person. newp2 = Person. new # To define the singleton method, you must first generate an instance object, and then add the object name and a period before the method name ". "def p2.talk puts" This talk method belongs to p2. "end def p2.laugh puts" laugh method belongs to p2. "end p1.talk # This is a Person class. p2.talk # This talk method belongs to p2. p2.laugh # laugh method belongs to p2.
This makes people have to think of the JavaScript Object creation syntax.var person = new Object();person.name = “Nicholas”;person.age = 29;person.job = “Software Engineer”;person.sayName = function(){alert(this.name);};Next we will introduce the JavaScript Object Model JavaScriptLiteral Translation dynamic type based on Prototype
Account := Object cloneAccount balance := 0Account deposit := method(amount, balance = balance + amount)account := Account cloneaccount deposit(10.00)account balance println
Let's take a look at the clone and new features of this person. In the original type programming, the object is cloned. But there is always a place to share all the things shared by objects. Therefore, in Io, an object starting with an uppercase letter represents a type, and Prototype is also known as Prototype in JavaScript. I think the prototype in JavaScript is a class. All methods and attributes defined in the prototype are shared. Each object can also have its own custom method. Once the methods or attributes defined on the object have the same name as those defined in Prototypefunction Person(){}Person.prototype.name = “Nicholas”;Person.prototype.age = 29;Person.prototype.job = “Software Engineer”;Person.prototype.sayName = function(){alert(this.name);};var person1 = new Person();var person2 = new Person();person1.name = “Greg”;alert(person1.name);//”Greg” - from instancealert(person2.name);//”Nicholas” - from prototypedelete person1.name;alert(person1.name);//”Nicholas” - from the prototypeIn Ruby, a Class is an instance of a Class. That is to say, a Class is an object. In fact, it is still a constant. Since the Class is an object, the Class can also use the instance method in the Class and its parent Class. The definitions of these instance methods are generic methods of objects, which is why classes and objects sometimes have equal operations. At the same time, this also leads to some differences in methods. For example, classes are used for class methods, while objects are used for instance methods. In JavaScript, JavaScript unifies functions and type variables and considers them to exist in a container. This container is called an attribute. Therefore, an object can be considered as an unordered attribute list. Another attribute can be simply regarded as a name and its value (more than that ). Such an object can also be viewed as a HA List.