Skip to content

Commit b9b2054

Browse files
committed
q22-q25 rewritten
1 parent e72a977 commit b9b2054

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

README.md

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ counterArray["C"] = 1;
687687
```
688688
### Answer
689689

690-
First of all, in case of JavaScript an associative array is the same as an object. There is no built-in function or property available to calculate the length an object, however we can write such a function ourselves.
690+
First of all, in case of JavaScript an associative array is the same as an object. Secondly, even though is no built-in function or property available to calculate the length/size an object, we can write such function ourselves.
691691

692692
#### Method 1
693693

@@ -742,7 +742,7 @@ function helloWorld(name) {
742742
helloWorld("JS Geeks"); // "hello world JS Geeks"
743743
```
744744

745-
Methods in JavaScript are nothing more than object properties that reference to a function.
745+
Methods in JavaScript are nothing more than object properties that are functions.
746746

747747
```javascript
748748
var obj = {
@@ -779,7 +779,7 @@ emp1.name; // "John Doe"
779779
emp1.age; // 28
780780
```
781781

782-
Unlike function calls and method calls, a constructor call `new Employee('John Doe', 28)` create a brand new object and passes it as the value of `this`, and implicitly returns the new object as its result.
782+
Unlike function calls and method calls, a constructor call `new Employee('John Doe', 28)` creates a brand new object and passes it as the value of `this`, and implicitly returns the new object as its result.
783783

784784
The primary role of the constructor function is to initialize the object.
785785

@@ -795,7 +795,9 @@ var person = new User("xyz")["location"] = "USA";
795795
console.log(person);
796796
```
797797

798-
The output of above code would be `USA`. Here `new User("xyz")` creates a brand new object and created property `location` on that and `USA` has been assigned to object property location and that has been referenced by the person.
798+
### Answer
799+
800+
The output of above code would be `"USA"`. Here `new User("xyz")` creates a brand new object and created property `location` on that and `USA` has been assigned to object property location and that has been referenced by the person.
799801

800802
Let say `new User("xyz")` created a object called `foo`. The value `"USA"` will be assigned to `foo["location"]`, but according to [ECMAScript Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-assignment-operators-runtime-semantics-evaluation) , pt 12.14.4 the assignment will itself return the rightmost value: in our case it's `"USA"`.
801803
Then it will be assigned to person.
@@ -816,14 +818,15 @@ foo["location"] = "USA";
816818

817819
## Question 24. What are Service Workers and when can you use them?
818820

821+
### Answer
822+
819823
It’s a technology that allows your web application to use cached resources first, and provide default experience offline, before getting more data from the network later. This principle is commonly known as Offline First.
820824

821825
Service Workers actively use promises. A Service Worker has to be installed,activated and then it can react on fetch, push and sync events.
822826

823827
As of 2017, Service Workers are not supported in IE and Safari.
824828

825829
## Question 25. What is the difference between a method and a function in javascript?
826-
827830
A function is a piece of code that is called by name and function itself not associated with any object and not defined inside any object. It can be passed data to operate on (i.e. parameter) and can optionally return data (the return value).
828831

829832
```javascript
@@ -854,28 +857,56 @@ A function can be self-invoking anonymous function or named self-invoking functi
854857

855858
In a case of named self-invoking anonymous function or anonymous self-invoking function, there is no need of call function explicitly.
856859

857-
A method is a piece of code that is called by name and that is associated with the object. Methods are funtions. In most respects it is identical to function call except for some key difference:
858-
- It is implicitly passed for the object for which it was called.
859-
- It is able to operate on data that is contained within the class (remembering that an object is an instance of a class- the class is the definition, the object is an instance of that)
860-
- Method call is always associated with object
860+
A method is a piece of code that is called by its name and that is associated with the object. Methods are functions. When you call a method like this `obj1.myMethod()`, the reference to `obj1` gets assigned (bound) to `this` variable. In other words, the value of `this` will be `obj1` inside `myMethod`.
861+
862+
Here are some examples of methods:
861863

864+
##### Example 1
862865
```javascript
863-
var methodObject = {
866+
var obj1 = {
864867
attribute: "xyz",
865-
display: function () { // Method
868+
myMethod: function () { // Method
866869
console.log(this.attribute);
867870
}
868871
};
869872

870-
// Call method
871-
methodObject.display();
873+
// Call the method
874+
obj1.display();
872875
```
873876

874-
Here methodObject is an object and display is a method which is associated with methodObject.
877+
Here `obj1` is an object and `myMethod` is a method which is associated with `obj1`.
878+
879+
##### Example 2
880+
In ES6 we have classes. There the methods will look like this:
881+
882+
```javascript
883+
class MyAwesomeClass {
884+
myMethod() {
885+
console.log("hi there");
886+
}
887+
}
875888

889+
const obj1 = new MyAwesomeClass();
890+
obj1.myMethod();
891+
```
876892

893+
Understand: the method is not some kind of special type of a function, and it's not about how you declare a function. It's the way we **call** a function. Look at that:
877894

895+
```javascript
896+
var obj1 = {
897+
prop1: "buddy"
898+
};
899+
var myFunc = function () {
900+
console.log("Hi there", this);
901+
};
902+
// let's call myFunc as a function:
903+
myFunc(); // will output "Hi there undefined" or "Hi there Window"
904+
905+
obj1.myMethod = myFunc;
906+
//now we're calling myFunc as a method of obj1, so this will point to obj1
907+
obj1.myMethod(); // will print "Hi there" following with obj1.
878908

909+
```
879910

880911
## Question 26. What is JavaScript Self-Invoking anonymous function or Self-Executing anonymous function.
881912
A `self-invoking` anonymous function also called `self-executing anonymous function` runs immediately or automatically when we define it and self-invoking anonymous function doesn't have any name at all. Self-Invoking anonymous function syntax:

0 commit comments

Comments
 (0)