Skip to content

Commit feb712a

Browse files
committed
adding few prototype related problems and explanation
1 parent 2417648 commit feb712a

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Extending Native Constructors. This is often considered a bad practice because if you create a custom String method and browsers in the future implement that method slightly differently, code may not work as you’d expect. Despite that, it is still a good exercise and worthwhile to learn for understanding prototypes.
2+
3+
// Write a function to replace a single space with a "-"
4+
5+
String.prototype.dasherize = function() {
6+
return this.replace(/\s/g, '-')
7+
}
8+
9+
console.log('Hello world'.dasherize());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Every JavaScript object has an internal property called [[Prototype]]. If you look up a property via obj.propName or obj['propName'] and the object does not have such a property - which can be checked via obj.hasOwnProperty('propName') - the runtime looks up the property in the object referenced by [[Prototype]] instead. If the prototype-object also doesn't have such a property, its prototype is checked i */
2+
3+
// basic way to print a given array's prototypes
4+
5+
let myArr = [1, 2, 3];
6+
7+
// console.log(Object.getPrototypeOf(myArr)) // => []
8+
9+
// console.log(Object.getOwnPropertyNames(myArr)) // => [ '0', '1', '2', 'length' ]
10+
11+
// Not a prototype method, but you can use Object.getPrototypeOf to traverse the prototype chain and then get the own property names of each of those objects.
12+
13+
console.log(Object.getPrototypeOf(Array.prototype)) // => {}
14+
15+
/* The prototype relations of JavaScript objects form a tree-shaped struc-
16+
ture, and at the root of this structure sits Object.prototype. It provides a few
17+
methods that show up in all objects, such as toString, which converts an ob-
18+
ject to a string representation. */
19+
20+
console.log(Object.prototype) // => {}
21+
22+
/* Many objects don’t directly have Object.prototype as their prototype, but
23+
instead have another object, which provides its own default properties. Func-
24+
tions derive from Function.prototype, and arrays derive from Array.prototype. */
25+
26+
console.log(Object.getPrototypeOf(isNaN) === Function.prototype) // => true as both will return [Function]
27+
28+
console.log(Object.getPrototypeOf([])) // = []
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Write a function to list Down All Prototype Properties of an Javascript Object
2+
3+
printPrototypeProperties = obj => {
4+
var result = [];
5+
6+
let objProp = Object.getOwnPropertyNames(obj)
7+
console.log(objProp)
8+
9+
// console.log(Object.keys(obj))
10+
11+
for (let i of objProp) {
12+
13+
if (result.indexOf(i) === -1 ) {
14+
result.push(i);
15+
}
16+
}
17+
return result;
18+
}
19+
20+
21+
let myObj = {
22+
"a": 1,
23+
"b": 2,
24+
"c": 3
25+
}
26+
27+
console.log(printPrototypeProperties(myObj));
28+
29+
30+
/* Object.getOwnPropertyNames vs Object.keys
31+
32+
https://stackoverflow.com/questions/22658488/object-getownpropertynames-vs-object-keys
33+
34+
35+
203
36+
down vote
37+
accepted
38+
There is a little difference. Object.getOwnPropertyNames(a) returns all own properties of the object a. Object.keys(a) returns all enumerable own properties. It means that if you define your object properties without making some of them enumerable: false these two methods will give you the same result.
39+
40+
It's easy to test:
41+
42+
var a = {};
43+
Object.defineProperties(a, {
44+
one: {enumerable: true, value: 'one'},
45+
two: {enumerable: false, value: 'two'},
46+
});
47+
48+
Object.keys(a); // ["one"]
49+
Object.getOwnPropertyNames(a); // ["one", "two"]
50+
51+
If you define a property without providing property attributes descriptor (meaning you don't use Object.defineProperties), for example:
52+
53+
a.test = 21;
54+
then such property becomes an enumerable automatically and both methods produce the same array.
55+
56+
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// https://youtu.be/OOC-ypVnHAY?t=10m35s - That Indian looking guy, who takes mock interview in YT.
2+
// Problem - write a function print() which will be implemented like this myArr.print() and it will print all the elements of the array
3+
4+
// SOL-1
5+
Array.prototype.print = function() {
6+
let resultArr = []
7+
this.forEach(i => resultArr.push(i))
8+
return console.log(resultArr.join());
9+
}
10+
11+
myArr = [1, 2, 3]
12+
myArr.print() // => 1,2,3
13+
14+
// SOL-2
15+
Array.prototype.print_1 = function() {
16+
return this.forEach(i => console.log(i))
17+
}
18+
19+
20+
myArr.print_1()
21+
22+
/* OUTPUT
23+
1
24+
2
25+
3 */

0 commit comments

Comments
 (0)