Javascript basic doubts oop or obp , scripting or programming language? - javascript

Recently I attended an interview where I was following question on javaScript
Q: is it object based or object oriented programming
Me :Object based , as there is no class and the first thing we create is an object to create other objects from it.
Q : Is it a scripting language or programming language?
Me: programming language(Q: Then why there is script in the name Me: Not sure)
Q: What object oriented features are there in it?
Me : Encapsulation and Inheritance (Q: Not polymorphism ? Me : No Q : but everything in javascript is late binding so why do you say it is not polymorphism is not there ME: Blank)
I had got theses answers from different tutorials online but but looks like he wasn't convinced with my answers . I did google them before posting a question over here, but still am confused as some sites say as my answers and some opposite to mine.
Could someone please help me clear my doubts ?

JavaScript is a object oriented Scripting language. It has the characteristics of all object oriented principles. (Abstractions, Encapsulations, Inheritance, Polymorphic (its dynamic typing is equal to polymorphism - late binding) etc), it uses prototypical inheritance instead of conventional inheritance.
JavaScript in general is called as scripting language. Any Language that runs in an environment is a scripting language. The scripting facilitates to access various entities or objects in that environment. (in case of browser, the browser itself is an environment, in case of back end, the node is an environment).

JavaScript is a programming language AND a scripting language. These are not opposites. Scripting language is typically intepreted and the opposite is compiled language, which JavaScript isn't. Even if a scripting language has got a JIT compiler, it is still a scripting language.

Related

Is Javascript any better than before as mentioned in the O'Reilly Javascript Patterns book for closures and anonymous functions?

This is a quote from the O'Reilly Javascript Patterns book:
JavaScript is also an unusual language. It doesn’t have classes, and
functions are first class objects used for many tasks. Initially the
language was considered deficient by many developers, but in more
recent years these sentiments have changed. Interestingly, languages
such as Java and PHP started adding features such as closures and
anonymous functions, which JavaScript developers have been enjoying
and taking for granted for a while.
and that's it. I really don't understand how Javascript was considered "deficient" before, and now is not, because other languages like Java or PHP has added closures and anonymous functions? Aren't they just generic computing concepts? Aren't they available in other languages like Ruby? So I don't really know how Javascript is now not "deficient" because Java and PHP added closures and anonymous functions as their features? Why is that?
I think what it's referring to is that in the past many developers considered JavaScript as a 'toy' language and only used it to do quick Web UI tasks like validation etc. without bothering to understand how the language really worked.
In recent years the 'hidden' features of JavaScript such as closures, prototypal inheritance etc. have come to the fore and people are now taking JavaScript much more seriously as a 'real' language.
So JavaScript was never really "deficient" but people may have thought that it was due to their misconceptions about the language.
I personally think it's a poor editing job.
The paragraph should have read (bold addition is mine and is just a suggestion on how
to read it):
JavaScript is also an unusual language. It doesn’t have classes, and
functions are first class objects used for many tasks. Initially the
language was considered deficient by many developers, but in more
recent years these sentiments have changed due to a better and more uniform browser support, extensive work done by various ECMA editions and evolution of various JavaScript frameworks. All, or much, of that change is a direct result of ever-expanding movement of software products to the Web and growing demand for a lightweight language for mobile applications (this one can be somewhat argued).
Interestingly, languages such as Java and PHP started adding features
such as closures and anonymous functions, which JavaScript developers
have been enjoying and taking for granted for a while.
For years, JavaScript has been known to only be a browser language, for (simple) features that required more dynamic and/or flexibility than offered by HTML and CSS.
For several years now, JavaScript evolved as a language and as platform, offering powerful libraries, which also removed certain cross-browser incompatibilities. With rise of these libraries, community actually only started to learn how to use some powerful concepts of JavaScript, such as closures and prototypal inheritance.
They have not been widely used nor known even to JavaScript developers before, since JavaScript is in its basics very simple language, and most developers didn't even have to learn it as you'd learn Python, C or Java - all they had to look at were tutorials and many of them didn't include these language features.
Duo to the increase in popularity, JavaScript (officially ECMAScript) has received several 'editions', where features have been added to the language itself. Latest edition (still under development) will introduce some radical novelties (considered are classes, module system, iterators, algebraic types among others), so JS will become even better, more structured and more easily maintainable language.
JavaScript is raising in popularity on server side also (node.js), and proves to be easy, fast and enough powerful language to do (almost) any kind of work. Node widely 'abuses' probably its best feature - asynchronous nature of JavaScript. That is not new, many languages have async libraries, but as Ryan Dahl said in one of his earliest speeches on node, JS is perfect for it. And mainly because of the closures and other concepts built into it.
So final answer would be the author of the book you're quoting has poorly expressed his thoughts or is simply wrong. It was a toy language and didn't mean to be used for such variety of use cases as it is today. But it evolved, just as Java or PHP did (don't forget PHP didn't have OOP support that seems so obvious now until v5). Other languages are just picking up goodies that JavaScript proved to be more efficient for certain use cases. Overall, my thoughts are that JavaScript hasn't been used properly from the start, and that's the case it had been called 'deficient'. Now developers got chance to explore its magnificence.

What “type” of language is JavaScript?

Programming languages are often classified by their structure. To date there are many types, including (but not limited to): Procedural, Structured, Object-Oriented, Concurrent, Functional, Event-Driven, Compiled, and Interpreted (Source). Some programming languages are a mixture of these constructs, while others may fall under only one of the headings listed above.
My question is: What type of language can JavaScript be classified as?
The Javascript Wikipedia entry tells you.
JavaScript (/ˈdʒɑːvəˌskrɪpt/), often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.
Paragraph three goes on to say:
As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative programming styles (emphasis added). It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).
Regarding your selection of Procedural, Structured, Object-Oriented, Concurrent, Functional, Event-Driven, Compiled, and Interpreted as "types" of a computer language, Javascript is quite powerful; it is at least (or can be) Procedural, Structured, Object-Oriented, Functional, Event-Driven and Interpreted.
However, as your source says, many languages do not fall into one category only, but can be used in different ways. Even for example Java could be used in a rather strict procedural way.
And although Javascript supports functional programming, it is not the lingua franca of functional programmers - but they could use it, if they wanted to.
Javascript is so versatile, because it is prototype-based and because you can pass functions as parameters to other functions, which really allows for some crazy programming techniques (read: HACKS) combined with dynamic creation of Javascript it can be really powerful and confusing as well :)
You might want to look up http://en.wikipedia.org/wiki/Prototype-based_programming for more explanations.
Mozilla Developer Network
...
JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes (see more about prototypical inheritance and ES2015 classes). JavaScript also supports functional programming — functions are objects, giving functions the capacity to hold executable code and be passed around like any other object.
...
JavaScript is an implementation of the ECMAScript language standard and is primarily used in the form of client-side JavaScript, implemented as part of a web browser in order to provide enhanced user interfaces and dynamic websites. This enables programmatic access to computational objects within a host environment.

Object Oriented Javascript

In the course of programming we encounter large javascript files which are open source and written in an object oriented manner( like JQuery ).
If we need to modify these files we have to have a basic knowledge of the members and the flow. If we have multiple files then the task is much more difficult.
Where do I start to get the flow of this??
First of all I think that you have to understand how JavaScript object orientation works, JavaScript OO is Prototype-based, in which classes are not present, and behavior reuse is implemented by prototyping.
I've seen that this can be hard to catch at the beginning for programmers that have been working on conventional class-based object-oriented languages (like C++, C#, Java, etc).
Recommended articles:
Introduction to Object-Oriented JavaScript
JavaScript: The World's Most Misunderstood Programming Language
Classical Inheritance in JavaScript
Private Members in JavaScript
Class-Based vs. Prototype-Based Languages
There are two things I would do:
Read. If there's documentation files, read those. If there's comments, read those. If neither of those help you, then go to the source and read that.
When you talk about open source Javascript, I assume you mean this JS is collected into some kind of project; all client-side JS is open source :P. In that case, the authors may be willing to tell you about their code. Locate their email on the project page, and ask them to give you a high-level overview of the code so you can start reading it and understanding it yourself. They probably won't be willing to hold your hand through the entire thing, but having that as a starting point would probably help.
I have a copy of
Javascript: The Good Parts
and
Javascript: The Definitive Guide
sitting on my desk right now. Incidentally these are the only two Javascript books which Douglas Crockford thinks are any good ;)
They'll teach you how Javascript works, specifically how its object model is different to most (but not all) other object-oriented languages.
Other than that, do check out all the articles on Crockford's website, as have already been mentioned in other answers.
I agree with allyourcode there's no magic trick. You have to read the code and read the docs. And if the docs are no good, maybe you should think about using a different framework.
A good start is in understanding the difference between traditional OO and Javascript's Prototype model. (Crockford has some articless that implement traditional OO for Javascript in order to contrast the behaviors.)

What scripting languages are similar to ECMA?

I am looking for an ECMAScript alternative to work as a scripting language for custom application logic. There are things I like about ECMA, especially the syntax for the newer spec(like AS3).
Unfortunately, the AS3 engine is not open source to be able to integrate in a project. I would like a scripting language that was designed for object oriented use.
Specifically, is there a language that has:
Statically typed variables(optional)
Classes, including public/private members
Inheritance, including Interfaces
Packages(optional)
Clean syntax
Must be able to interface as an internal scripting language for an application(like Javascript for a browser), can not be an external system call.
Things I would rather do without
The messy ECMA prototype object
What languages that you know about fit this profile? I've had difficulty finding a quality scripting language that was designed for good object oriented design.
In Java the best ECMAScript (Javascript) option is to embed Rhino. I don't find the prototype-based inheritance a deal killer, as with a bit of discipline you can make it behave almost like class-based inheritance.
If you want something that interoperates very closely with Java, go with Groovy. If you need JVM compatibility, also look into Jython (python on the JVM), Jruby (Ruby on the JVM) and Scala (a functional language on the JVM). If you don't care about Java and JVM compatibility, look at Ruby, Python, and Erlang. Clojure is a dialect of Lisp on the JVM.
Going further afield, TCL (Tool Command Language) lets you embed an interpreter in C/C++ code, there are many embeddable Lisp and Scheme interpreters, etc.
If you want a scripting language that works like ECMAScript, why not use ECMAScript? There are many Open Source implementations, just take a look at the list on Wikipedia.
I'd recommend either Python or Ruby. Neither are like ECMA, but I learned them after JavaScript, and they were a snap to pick up. Plus, they are more powerful languages, making it a better alternative to using a JavaScript engine inside of your application (Rhino for Java).
Python
Forces clean syntax (almost like English while is not False:)
Multiple inheritance (no interfaces)
Interpreter can be extended using C/C++ (possibly used for your adapters, if needed)
Ruby
Syntax is supposed to be close to English (unless conditional, until loop)
Everything is an object
Only supports single inheritance, but uses Mixins to add functionality
Both
Classes
Can be embedded in another application
Private members
Packages
Lua - everything you want and more in ~100KB
See this page for comparison betwen Lua and other mentioned languages.
Haxe on Neko looks like the exact thing you want. I don't know how embeddable nekovm is, but it is opensource so you can fiddle with it. http://haxe.org
The Ruby interpreter can also be embedded within C programs, and may be considered by some to be more object-oriented than Python.
We use ECMAscript as an extension language for the software product I work on and it works quite well. Being a standard (and popular) language, it's nice to be able to point our users to the copious off the shelf documentation for the language. We built our own ECMAscript compiler that translates into bytecode for the platform we are running on, but if I had it to do over again, I'd seriously consider embedding Google's V8 ECMAscript engine (in fact, I'd even consider building our entire app on it).
I'd go with something fairly mainstream to simplify things. Having read your requirements, I'd recommend Python. It doesn't really have interfaces in the Java/C# way, but it doesn't really need them, either. Other than that, it should be a good fit.
QtScript is ECMAScript. You don't mention what the main part of your application is written in, but I suppose it's not Qt, or you'd already know about QtScript.

Scripting and Programming [duplicate]

This question already has answers here:
When is a language considered a scripting language? [closed]
(40 answers)
Closed 8 years ago.
Where does Scripting ends and Programming begins? ActionScript 3 and JavaScript/JScript are so different...
The distinction was meaningful once, but the line is getting increasingly blurred to the point where I dont think it is useful today.
historically, scripting languages are interpreted, programming langauages are compiled. But with the advent of VM's and JIT, the line is increasingly blurred.
scripting is when an existing application or tool is manipulated programatically (e.g. office macros), rather than building an app from scratch. Again the line is getting blurred, because libraries and frameworks means that everybody is building on existing tools. Also, API's allow you to use traditional compiled languages to manipulate applications, eg. in Office you can now automate using .net, which I supppose means you can write Excel macros in managed C++.
languages tend to outgrow their niche. Perl was concieved as a scripting language for text manipulation, but has since grown into a full-fledged programming language.
scripting languages have traditionally been higher-level with features like garbage collection and implicit typing which allowed the developer to focus on the task rather than the metal. Real programming languages like C forced you to be more concerned about memory management and machine architecture. Again, the line is being blurred by newer languages. For example, C# has high-level features like garbage-collection and dynamic types, and low-level features like pointers.
For these reasons scripting has been seen as more accessible, where programming has been seen as more demanding and hard-core. Therefore the term scripting can be used disparagingly, like in "JavaScript is just a scripting languague, not a real programming language" or "he is just a scripter, not a real programmer".
Is there a difference? Youre just writing software in different languages for different run-time environments.
It reminds me of an old definition of the difference between prose and poetry which went something like:
There once was a lady from Glass
Who went into water up to her knees.
That's prose. If she had gone deeper, it would have been poetry.
It strikes me that the difference between scripting and programming is a matter of degree, not what language you are using. I've seen people write some pretty sophisticated programs using the bash shell, and others write trivial things using C. Which is the programmer and which is the scripter?
When you're able to write something that's more than a quick hack or something knocked together to get the immediate job done, when you've written something with an eye towards craftsmanship and maintainability, then you've written a program.
The German Wikipedia says:
Aus architektonischer Sicht werden
Skriptsprachen verwendet, um aus
bestehenden Komponenten ein
Anwendungsprogramm zu bauen. Die
Komponenten selbst werden in einer
Programmiersprache entwickelt.
Translation:
From an architectural point of view, script languages are used to
build an application program from
existing components. The components
themselves are developed using
programming languages.
Script languages use components that are already there to make programs. These components are written in programming languages. :)
Think about shell scripting... There you use little programs "mostly" written in C.
Programming is programming.
People often differentiate between scripting languages and compiled languages, but the distinction isn't that useful IMO.
I.e. can compile many scripting languages and interpret many compiled languages.
This question is similar to these past questions:
Programming vs scripting — what’s your definition?
What’s the difference between a “script” and an “application”?
A script, in common usage, is a sequence of actions ("enter stage left"), like a recipe.
For example, a testing script is a simple sequence of actions to perform. It has zero cyclometric complexity.
Programs are more general, and can do anything. They should be modular and designed for reuse.
As software expands, scripts become more complex, but at heart they are imperative recipes, designed for a single purpose.
In one of the wargaming simulations I've worked on, one of the big selling points was that it wasn't scripted, but each entity in the simulation was an autonomous actor which responded to events.
Whether or not the runtime is interpreted, compiled, jited or whatever doesn't matter. Awk scripts were compiled. Lisp programs were often interpreted. Often the unit test scripts for a program are written in the same language as the program.
If I had to point to a language difference, it would be the presence of a module system. Apart from C, all 'proper' programming languages have a module system. JavaScript is getting a module system as part of the ServerJS working group, and is obviously mature enough for applications programming, so is now neither anything to do with Java nor exclusively a scripting language.
The difference should really be made between scripted (interpreted) and compiled code. Programming, I think, is just the general term of giving instructions to a computer or machine.
Programming Languages: Languages that are compiled before they are deployed.
Scripting Languages: Languages that are code when they are deployed and are interpreted on the fly when they are requested.
In the end it's all the same sh*t, they are all programming languages
Two definitions that I find particularly useless are:
Programming depends on the code's being compiled before deployment
Scripting means that the code is "only" used to connect components
I was initially tempted to draw the line around the intention with which the code was written. I would have called one-off with limited lifetime and scope a script and everything else a program. But then, I have written quite a few such programs in C ... and I wouldn't call anything in C as scripted.
My current working definition is this: Scripted code tend to be limited in scope, small, and (hopefully) simple to understand and replace. Everything else is programmed. :-)
Remember that most modern scripting languages have frameworks or backends with some built-in compiling to pre-build and cache the scripts. (see PHP with some optimizers like Zend or eAccelerator, template engines...)
Modern scripting languages like Actionscript3, PHP, Javascript allowing most of modern design pattern (like MVC) and some OOP paradigms - and some advanced designs like "closures". So you have all for "programming".
Rich Internet Applications (RIA) needs a lot of programming skills, the same with RIA running as a desktop application (Flash/Flex with Adobe Air). This has nothing todo anymore with some easy scripting embedded in a website. This are complex applications needing a lot programming skills.
Because most of the scripting languages uses some "backend" to get/retrieve data from databases, this applications are often not easy to develop because of much side effects. People must have knowledge with server stuff to setup their development environment, other scripting languages like PHP for the backends and so so on. Implement some same thing with C++ and a good framework like Qt, wxWidgets are sometimes easier and needs not much more programming skills.
Scripting and programming are for me the same. Yes, you could say "writing a onclick handler for a button on a website is scripting... writing a videoplayer or game with Flex/Flash and Actionscript3 is programming"?
There a differences between a scripting language and a compiled language (host access, sandbox, performance...). But its all programming.

Categories

Resources