What type of language is JavaScript
I'm in a begi开发者_如何学运维nning HTML class and our textbook says that JavaScript is an object-based scripting language. On our quiz, one of the questions was "JavaScript is an object-oriented scripting language, True or False." I put False because it's my understanding that object-based and object-oriented are two different things. I got the answer wrong.
Is JavaScript indeed an object-oriented language?
Thanks for any clarification you can give!
JavaScript uses prototype-based programming, which is a type of object oriented programming. Prototypes are a way of reusing behaviors by cloning existing objects instead of doing class based inheritance. It's different enough from the standard class based object oriented programming that few people bother to learn it well enough to make good use of it.
http://en.wikipedia.org/wiki/Prototype-based_programming is a useful reference.
The definition of Object Oriented is sometimes subjective. For some, any language that deals with "Objects", is Object Oriented. For others, the entire language and its construct must utilize objects to be counted as Object Oriented (think SmallTalk or Java).
In javascript, you're able to write a script that has no objects in it or you can create heavily object oriented code. Whether or not you call it Object Oriented is really dependant on what school of thought you follow.
I'd say that object-oriented is not a feature of programming languages, it is a feature of code. Code does not grow object-oriented, prototype-based or functional simply because it is written in a specific language, it obtains such a quality only if the author use that style.
Sure, it makes sense to call a language like Java object-oriented since the language is designed specifically for that paradigm, but JavaScript works well with a load of different paradigms, so you really can't put a sticker on it.
This answer I am sure has been answered elsewhere.... object-based and object-orientated are the same thing, used interchangeably. There is actually no difference in the terminology - some prefer one over the other.
To answer your question, yes, Javascript is object-orientated or object-based.
Javascript is a superset of ECMAScript, which is a multi-paradigm ( functional, procedural ), prototype-based, functional, imperative scripting language with duck/weak/dynamic typing and is influenced by Perl, C, Python, Java, Scheme.
ECMAScript was created by Brendan Eich. Source: http://en.wikipedia.org/wiki/ECMAScript
It adopts C constructs such as if
and else
and for
, but also has closures
and lambdas
from Scheme, as well as prototype-based
inheritance from Self so it can be used in an OO way.
Object-based, object-oriented, basically the same thing. It's my impression that object-oriented is a bit stricter than object-based.
Object-based implies that the language uses the idea of encapsulating state and operations inside "objects".
Object-oriented means that the language is designed for programming with objects.
The difference is pretty minor. See the wikipedia articles on object-based language and object-oriented programming to get a finer idea of the difference.
The term Object-oriented programming (OOP) is not very well-defined in general. See Jonathan Rees note on OO: Rees on OO. Javascript has an OO model based on a concept named prototypes. The prototype-based model is different from the model used in Java or C++, but it certainly falls underneath the general umbrella that is OOP-based languages.
Perhaps the term "object-based" was used to underline this fact about javascript. It uses objects, but not in the same vein as Java or C++ (or C#) does.
The object-oriented fundamentals are there but are structured differently from other more common object-oriented languages like C# and Java. You can create object constructors and methods, and access and retrieve object properties. JavaScript was designed to be a completely object-oriented language from the start but somehow was influenced by Perl and Python and therefore the current programmatic idioms.
What is wrong with this object-oriented notation in javascript?
function Class( name, teacher ) {
this.name = name;
this.teacher = teacher;
}
If your quiz defined neither ‘object-oriented’ nor ‘scripting language’, I can’t possibly see how you can be expected to answer the question.
精彩评论