Ver Mensaje Individual
  #18 (permalink)  
Antiguo 27/10/2013, 15:05
Avatar de ryugen
ryugen
Colaborador
 
Fecha de Ingreso: agosto-2008
Ubicación: Rosario, Santa Fe
Mensajes: 350
Antigüedad: 15 años, 7 meses
Puntos: 187
Respuesta: En javascript que es un objeto

En estos casos nada mejor que recurrir a la especificación:
http://www.ecma-international.org/pu...T/Ecma-262.pdf
(Los extractos que voy a citar son de la paǵina 3 y 4)
Cita:
ECMAScript does not use classes such as those in C++, Smalltalk, or Java. Instead objects may be created in various ways including via a literal notation or via constructors which create objects and then execute code that initialises all or part of them by assigning initial values to their properties. Each constructor is a function that has a property named ―prototype‖ that is used to implement prototype-based inheritance and shared properties. Objects are created by using constructors in new expressions; for example, new Date(2009,11) creates a new Date object. Invoking a constructor without using new has consequences that depend on the constructor. For example, Date() produces a string representation of the current date and time rather than an object.
Every object created by a constructor has an implicit reference (called the object‘s prototype) to the value of its constructor‘s ―prototype‖ property. Furthermore, a prototype may have a non-null implicit reference to its prototype, and so on; this is called the prototype chain. When a reference is made to a property in an object, that reference is to the property of that name in the first object in the prototype chain that contains a property of that name. In other words, first the object mentioned directly is examined for such a property; if that object contains the named property, that is the property to which the reference refers; if that object does not contain the named property, the prototype for that object is examined next; and so on.
In a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behaviour. In ECMAScript, the state and methods are carried by objects, and structure, behaviour, and state are all inherited.
All objects that do not directly contain a particular property that their prototype contains share that property and its value.
(...)
Unlike class-based object languages, properties can be added to objects dynamically by assigning values to them. That is, constructors are not required to name or assign values to all or any of the constructed object‘s properties.
En definitiva, JavaScript no está pensado para soportar los POO clásica que conocemos de Java, C++ y otros lenguajes.

Sino que los objetos en JavaScript están basados en prototipos (simplifcando la creación a partir de la clonación de otro objeto de esa clase). En definitiva cuando yo creo un nuevo objeto en JavaScript, estoy clonando al objecto object (prototipo/patrón de todo otro objeto). Esto da a lugar para que en JavaScript los estados se puedan "heredar".

Otras de las diferencias importantes que bien marca la especificación es que los objetos son dinámicos, es decir que sus propiedades pueden añadirse dinamicamente, osea que no existe una definición previa