Basic JavaScript Flashcards
Understand the basics of JavaScript Programming
What is JavaScript and what programming paradigms does it support?
JavaScript is a versatile programming language initially designed for web browsers but now used in various environments. It supports object-oriented, imperative, and functional programming styles.
How has ECMAScript influenced the development of JavaScript?
ECMAScript, as the standard governing JavaScript, has progressively introduced more formal language features like classes and arrow functions (=>), which have enhanced the structure and capabilities of JavaScript, moving it from relying on programming conventions to having built-in language features.
What is Functional Programming and what are its key features?
Functional Programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. Its key features include the use of functions as first-class citizens, avoidance of side effects, support for higher-order functions, function composition, and recursion.
How does Functional Programming differ from other programming paradigms in terms of handling data and functions?
Functional Programming differs from other paradigms by treating functions as first-class citizens, meaning functions can be passed as arguments, returned from other functions, and assigned to variables. It avoids mutable data and changing state, focusing on immutable data and pure functions that do not cause side effects, contrasting with paradigms like imperative programming that rely on changing state.
Define Object-Oriented Programming and its core concepts.
Object-Oriented Programming is a paradigm based on the concept of “objects” containing data and methods. Its core concepts include encapsulation (bundling data and methods that operate on the data within objects), inheritance (deriving new classes from existing ones), and polymorphism (the ability of objects of different classes to respond to method calls of the same name).
Compare and contrast Object-Oriented Programming with Imperative Programming.
Object-Oriented Programming focuses on objects and their interactions, using classes to define data structures and behaviors, promoting data encapsulation and reusability. Imperative Programming, on the other hand, focuses on explicitly detailing the steps a computer must take to reach a desired state, often using a sequence of commands, loops, and conditional statements. OOP is more about modeling and organizing complex systems, while Imperative Programming is about defining precise operations.
What is Imperative Programming and how does it operate?
Imperative Programming is a programming paradigm that uses statements to change a program’s state. It involves writing a sequence of commands for the computer to perform, focusing on describing how a program operates. This style of programming often employs loops, conditional statements, and variables to detail the steps required to achieve a desired outcome.
Give an example of a programming language that is primarily imperative and explain its characteristics.
C is a prime example of an imperative programming language. It focuses on how to perform tasks with commands, loops, and conditionals. C requires the programmer to specify exact steps to achieve a result, often involving detailed management of computer resources, like memory allocation, which exemplifies the hands-on approach of imperative programming.
Given the JavaScript code snippet where a method “increment” is added to an object “obj”, explain how method invocation works and what “this.count += amount;” does within the method.
In the given JavaScript code, the method “increment” is added to the object “obj”. When “obj.increment(1)” is called, the method invocation binds “this” to the object “obj”, allowing the method to access “obj’s” properties. The line “this.count += amount;” increments the “count” property of “obj” by the given “amount”. The first call “obj.increment(1)” adds 1 to “count”, returning 1. The second call “obj.increment(3)” adds 3, returning 4, as “count” is now 4.
Explain how the “this” keyword works inside methods and non-method functions in JavaScript. Provide examples for both cases.
In JavaScript, within a method, “this” refers to the object the method is part of. For example, in “o.aMethod()”, “this” refers to “o”, allowing access to its properties, such as adding “newProp”. In non-method functions, “this” refers to the global object in a non-strict mode (like the window object in browsers) but becomes undefined in strict mode. This distinction prevents “this” in non-method functions from inadvertently accessing or modifying the global scope.
Explain how functions in JavaScript can act as objects and discuss the example of the “plus1” function having a property “invocations”.
In JavaScript, functions can be treated as objects, allowing them to have properties. In the “plus1” function example, the property “invocations” is used to count how many times the function has been called. Each time “plus1” is called, the “invocations” property is incremented. This ability to store and update properties within the function itself is similar to static or class properties in object-oriented languages, where the property is shared and accessible across different instances of the class.
Describe the functionality of the methods toString(), call(), apply(), and bind() in JavaScript functions. Provide examples demonstrating their use.
In JavaScript, the toString() method of a function returns the function’s source code as a string. For example, “func.toString()” returns the function definition. The call() method calls a function with a specified “this” value and individual arguments, e.g., “func.call({t: 1}, 2)”. The apply() method is similar but takes arguments as an array, e.g., “func.apply({t: 2}, [2])”. The bind() method creates a new function with a pre-specified “this” value and arguments, e.g., “let newFunc = func.bind({z: 2}, 3); newFunc()” prints “{ z: 2 } 3”.
Explain how functions can act as classes and constructors in JavaScript, using the “Rectangle” function as an example. Discuss why defining methods within the constructor is not the most efficient way.
In JavaScript, functions can be used to define classes, acting as constructors to create instances. For example, the “Rectangle” function acts as a constructor for rectangles. When used with “new”, it creates a Rectangle object with specific “width” and “height”. However, defining a method like “area” within the constructor means every instance has its own copy of the method, which is less efficient than defining it on the prototype, where all instances can share a single copy of the method.
Explain prototype-based inheritance in JavaScript and how property access and updates work in this context.
In JavaScript, prototype-based inheritance allows objects to inherit properties from their prototype objects. When a property is accessed, JavaScript searches up the prototype chain until the property is found. For property updates, if the property is not on the object, it is created on the object itself. This design allows objects to inherit shared properties from their prototypes while still being able to have unique properties of their own.
Explain the use of prototypes in JavaScript, using the Rectangle function and its area method as an example.
In JavaScript, prototypes allow methods and properties to be shared across all instances of a function. The “Rectangle” function uses its prototype to share the “area” method with all its instances. This is memory-efficient as the method is stored only once on the prototype. However, changes to the prototype dynamically affect all instances, which highlights the need for cautious modifications.