You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45-14Lines changed: 45 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -687,7 +687,7 @@ counterArray["C"] = 1;
687
687
```
688
688
### Answer
689
689
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.
691
691
692
692
#### Method 1
693
693
@@ -742,7 +742,7 @@ function helloWorld(name) {
742
742
helloWorld("JS Geeks"); // "hello world JS Geeks"
743
743
```
744
744
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.
746
746
747
747
```javascript
748
748
var obj = {
@@ -779,7 +779,7 @@ emp1.name; // "John Doe"
779
779
emp1.age; // 28
780
780
```
781
781
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.
783
783
784
784
The primary role of the constructor function is to initialize the object.
785
785
@@ -795,7 +795,9 @@ var person = new User("xyz")["location"] = "USA";
795
795
console.log(person);
796
796
```
797
797
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.
799
801
800
802
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"`.
801
803
Then it will be assigned to person.
@@ -816,14 +818,15 @@ foo["location"] = "USA";
816
818
817
819
## Question 24. What are Service Workers and when can you use them?
818
820
821
+
### Answer
822
+
819
823
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.
820
824
821
825
Service Workers actively use promises. A Service Worker has to be installed,activated and then it can react on fetch, push and sync events.
822
826
823
827
As of 2017, Service Workers are not supported in IE and Safari.
824
828
825
829
## Question 25. What is the difference between a method and a function in javascript?
826
-
827
830
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).
828
831
829
832
```javascript
@@ -854,28 +857,56 @@ A function can be self-invoking anonymous function or named self-invoking functi
854
857
855
858
In a case of named self-invoking anonymous function or anonymous self-invoking function, there is no need of call function explicitly.
856
859
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:
861
863
864
+
##### Example 1
862
865
```javascript
863
-
varmethodObject= {
866
+
varobj1= {
864
867
attribute:"xyz",
865
-
display:function () { // Method
868
+
myMethod:function () { // Method
866
869
console.log(this.attribute);
867
870
}
868
871
};
869
872
870
-
// Call method
871
-
methodObject.display();
873
+
// Call the method
874
+
obj1.display();
872
875
```
873
876
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
+
classMyAwesomeClass {
884
+
myMethod() {
885
+
console.log("hi there");
886
+
}
887
+
}
875
888
889
+
constobj1=newMyAwesomeClass();
890
+
obj1.myMethod();
891
+
```
876
892
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:
877
894
895
+
```javascript
896
+
var obj1 = {
897
+
prop1:"buddy"
898
+
};
899
+
varmyFunc=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.
878
908
909
+
```
879
910
880
911
## Question 26. What is JavaScript Self-Invoking anonymous function or Self-Executing anonymous function.
881
912
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