Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 473ac64

Browse files
Object-Methods✅
1 parent c1c1bbc commit 473ac64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1050
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let a1 = {
2+
name: "Suhail",
3+
age: 20,
4+
};
5+
6+
let newA1 = {};
7+
8+
let copy = Object.assign(newA1, a1);
9+
console.log(newA1);//{ name: 'Suhail', age: 20 }
10+
newA1.ok = "Hi"
11+
console.log(newA1); //{ name: 'Suhail', age: 20, ok: 'Hi' }
12+
13+
console.log(copy == newA1); // true
14+
console.log(copy); //{ name: 'Suhail', age: 20, ok: 'Hi' }
15+
16+
// It copies whole object into an new Object with a referncene one
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
JavaScript Object getOwnPropertyNames()
3+
The JavaScript Object.getOwnPropertyNames() method returns an array of all properties found in a given object.
4+
5+
The syntax of the getOwnPropertyNames() method is:
6+
7+
Object.getOwnPropertyNames(obj)
8+
The getOwnPropertyNames() method, being a static method, is called using the Object class name.
9+
10+
getOwnPropertyNames() Parameters
11+
The getOwnPropertyNames() method takes in:
12+
13+
obj - The object whose enumerable and non-enumerable properties are to be returned.
14+
Return value from getOwnPropertyNames()
15+
Returns an array of strings that corresponds to the properties found directly in the given object.
16+
Note: Object.getOwnPropertyNames() returns all own properties of the object while Object.keys() returns all enumerable own properties.
17+
18+
Example: Using getOwnPropertyNames()
19+
// array object
20+
let arr = ["a", "b", "c"];
21+
console.log(Object.getOwnPropertyNames(arr)); // [ '0', '1', '2', 'length' ]
22+
23+
// array-like objects
24+
let obj = { 65: "A", 66: "B", 67: "C" };
25+
console.log(Object.getOwnPropertyNames(obj)); // [ '65', '66', '67' ]
26+
27+
// non-enumerable properties are also returned
28+
let obj1 = Object.create(
29+
{},
30+
{
31+
getValue: {
32+
value: function () {
33+
return this.value;
34+
},
35+
enumerable: false,
36+
},
37+
}
38+
);
39+
obj1.value = 45;
40+
41+
console.log(Object.getOwnPropertyNames(obj1)); // [ 'getValue', 'value' ]
42+
43+
44+
45+
46+
47+
48+
49+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
JavaScript Object getOwnPropertySymbols()
3+
The JavaScript Object.getOwnPropertySymbols() method returns an array of all symbol properties found in a given object.
4+
5+
The syntax of the getOwnPropertySymbols() method is:
6+
7+
Object.getOwnPropertySymbols(obj)
8+
The getOwnPropertySymbols() method, being a static method, is called using the Object class name.
9+
10+
getOwnPropertySymbols() Parameters
11+
The getOwnPropertySymbols() method takes in:
12+
13+
obj - The object whose symbol properties are to be returned.
14+
Return value from getOwnPropertySymbols()
15+
Returns an array of all symbol properties found directly upon the given object.
16+
Note: Object.getOwnPropertySymbols() returns all symbol properties of the object while Object.getOwnPropertyNames() returns the string properties
17+
18+
Example: Using getOwnPropertySymbols()
19+
let obj = {};
20+
let a = Symbol("a");
21+
let b = Symbol.for("b");
22+
23+
obj[a] = "localSymbolValue";
24+
obj[b] = "globalSymbolValue";
25+
26+
// returns an array of symbol
27+
let objectSymbols = Object.getOwnPropertySymbols(obj);
28+
29+
console.log(objectSymbols.length); // 2
30+
console.log(objectSymbols); // [Symbol(a), Symbol(b)]
31+
console.log(objectSymbols[0]); // Symbol(a)
32+
33+
34+
35+
36+
37+
38+
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Javascript Object.getPrototypeOf()
3+
The JavaScript Object.getPrototypeOf() method returns the prototype of the specified object.
4+
5+
The syntax of the getPrototypeOf() method is:
6+
7+
Object.getPrototypeOf(obj)
8+
The getPrototypeOf() method, being a static method, is called using the Object class name.
9+
10+
getPrototypeOf() Parameters
11+
The getPrototypeOf() method takes in:
12+
13+
obj - The object whose prototype is to be returned.
14+
Return value from getPrototypeOf()
15+
Returns prototype of the given object.
16+
Returns null If there are no inherited properties.
17+
Example: Using Object.getPrototypeOf()
18+
let proto = {};
19+
let obj = Object.create(proto);
20+
21+
console.log(Object.getPrototypeOf(obj) === proto); // true
22+
23+
console.log(Object.getPrototypeOf("JavaScript"));
24+
25+
// function
26+
let func1 = function () {};
27+
28+
// creating object from function
29+
let object1 = Object.create(func1);
30+
31+
console.log(Object.getPrototypeOf(func1) === Object.getPrototypeOf(object1)); // false
32+
33+
// Object.create() creates object with given object as prototype
34+
console.log(func1 === Object.getPrototypeOf(object1)); // true
35+
36+
37+
38+
39+
40+
41+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
JavaScript Object hasOwnProperty()
3+
The JavaScript Object hasOwnProperty() method checks if the object has the given property as its own property.
4+
5+
The syntax of the hasOwnProperty() method is:
6+
7+
obj.hasOwnProperty(prop)
8+
Here, obj is an object.
9+
10+
hasOwnProperty() Parameters
11+
The hasOwnProperty() method takes in:
12+
13+
prop - The String name or Symbol of the property to test.
14+
Return value from hasOwnProperty()
15+
Returns a Boolean indicating whether or not the object has the specified property as its own property.
16+
Notes:
17+
18+
Unlike the in operator, this method does not check for a property in the object's prototype chain.
19+
hasOwnProperty returns true even if the value of the property is null or undefined.
20+
Example: Using hasOwnProperty()
21+
const obj = {};
22+
obj.property1 = 42;
23+
24+
console.log(obj.hasOwnProperty("property1")); // true
25+
26+
console.log(obj.hasOwnProperty("property2")); // false
27+
28+
// Inherited properties return false
29+
console.log(obj.hasOwnProperty("toString")); // false
30+
31+
32+
33+
34+
35+
36+
*/

‎9. Object-Methods/14. Object.is().js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Javascript Object.is()
3+
The JavaScript Object.is() method checks if two values are the same value.
4+
5+
The syntax of the is() method is:
6+
7+
Object.is(value1, value2)
8+
The is() method, being a static method, is called using the Object class name.
9+
10+
is() Parameters
11+
The is() method takes in:
12+
13+
value1 - The first value to compare.
14+
value2 - The second value to compare.
15+
Return value from is()
16+
Returns a Boolean indicating whether or not the two arguments are the same value.
17+
Two values are the same if one of the following holds:
18+
19+
both undefined
20+
both null
21+
both true or both false
22+
both strings of the same length with the same characters in the same order
23+
both the same object (means both object have same reference)
24+
both numbers and
25+
both +0
26+
both -0
27+
both NaN
28+
or both non-zero and both not NaN and both have the same value
29+
Example: Using Object.is()
30+
// Objects with the same values
31+
console.log(Object.is("JavaScript", "JavaScript")); // true
32+
// Objects with different values
33+
console.log(Object.is("JavaScript", "javascript")); // false
34+
35+
console.log(Object.is([], [])); // false
36+
37+
let obj1 = { a: 1 };
38+
let obj2 = { a: 1 };
39+
console.log(Object.is(obj1, obj1)); // true
40+
console.log(Object.is(obj1, obj2)); // false
41+
42+
console.log(Object.is(null, null)); // true
43+
44+
// Special Cases
45+
console.log(Object.is(0, -0)); // false
46+
console.log(Object.is(-0, -0)); // true
47+
console.log(Object.is(NaN, 0 / 0)); // true
48+
49+
50+
51+
52+
53+
54+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
JavaScript Object.isExtensible()
3+
The JavaScript Object.isExtensible() method checks if an object is extensible i.e. new properties can be added to it.
4+
5+
The syntax of the isExtensible() method is:
6+
7+
Object.isExtensible(obj)
8+
The isExtensible() method, being a static method, is called using the Object class name.
9+
10+
isExtensible() Parameters
11+
The isExtensible() method takes in:
12+
13+
obj - The object which should be checked.
14+
Return value from isExtensible()
15+
Returns a Boolean indicating whether or not the given object is extensible.
16+
Note: An object can be marked as non-extensible using Object.preventExtensions(), Object.seal(), or Object.freeze().
17+
18+
Example: Using isExtensible()
19+
// New objects are extensible.
20+
let empty = {};
21+
console.log(Object.isExtensible(empty)); // true
22+
23+
Object.preventExtensions(empty);
24+
console.log(Object.isExtensible(empty)); // false
25+
26+
// Sealed objects are by definition non-extensible.
27+
let sealed = Object.seal({});
28+
console.log(Object.isExtensible(sealed)); // false
29+
30+
// Frozen objects are also by definition non-extensible.
31+
let frozen = Object.freeze({});
32+
console.log(Object.isExtensible(frozen)); // false
33+
34+
35+
36+
37+
38+
39+
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Javascript Object.isFrozen()
3+
The JavaScript Object.isFrozen() checks if an object is frozen.
4+
5+
A frozen object can no longer be changed. Freezing an object prevents:
6+
7+
New properties from being added to the object.
8+
Existing properties to be removed from the object.
9+
Changing the enumerability, configurability, or writability of existing properties.
10+
Changing values of the existing object properties and prototype.
11+
The syntax of the isFrozen() method is:
12+
13+
Object.isFrozen(obj)
14+
The isFrozen() method, being a static method, is called using the Object class name.
15+
16+
isFrozen() Parameters
17+
The isFrozen() method takes in:
18+
19+
obj - The object which should be checked.
20+
Return value from isFrozen()
21+
Returns a Boolean indicating whether or not the given object is frozen.
22+
Example: Using isFrozen()
23+
// new objects are extensible, so not frozen
24+
console.log(Object.isFrozen({ name: "JavaScript" })); // false
25+
26+
// preventing extensions only does not make frozen
27+
// property is still configurable
28+
let obj = { a: 1 };
29+
Object.preventExtensions(obj);
30+
console.log(Object.isFrozen(obj)); // false
31+
32+
// deleting property
33+
delete obj.a;
34+
console.log(Object.isFrozen(obj)); // true -> vacuously frozen
35+
36+
let newObj = { b: 2 };
37+
// make non-extensible
38+
Object.preventExtensions(newObj);
39+
// make non-writable
40+
Object.defineProperty(newObj, "b", {
41+
writable: false,
42+
});
43+
// properties are still configurable
44+
console.log(Object.isFrozen(newObj)); // false
45+
46+
// using freeze()
47+
let frozen = { 65: "A" };
48+
49+
Object.freeze(frozen);
50+
console.log(Object.isFrozen(frozen)); // true
51+
52+
53+
54+
55+
56+
57+
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Javascript Object.isPrototypeOf()
3+
The JavaScript Object.isPrototypeOf() method checks if an object exists in another object's prototype chain.
4+
5+
The syntax of the isPrototypeOf() method is:
6+
7+
prototypeObj.isPrototypeOf(object)
8+
Here, prototypeObj is an object.
9+
10+
isPrototypeOf() Parameters
11+
The isPrototypeOf() method takes in:
12+
13+
object - The object whose prototype chain will be searched.
14+
Return value from isPrototypeOf()
15+
Returns a Boolean indicating whether the calling object lies in the prototype chain of the specified object.
16+
Note: isPrototypeOf() differs from instanceof operator as it checks the object prototype chain against prototypeObj not prototypeObj.prototype.
17+
18+
Example: Using Object.isPrototypeOf()
19+
let obj = new Object();
20+
console.log(Object.prototype.isPrototypeOf(obj)); // true
21+
console.log(Function.prototype.isPrototypeOf(obj.toString)); // true
22+
23+
console.log(Array.prototype.isPrototypeOf([2, 4, 8])); // true
24+
25+
// define object
26+
let Animal = {
27+
makeSound() {
28+
console.log(`${this.name}, ${this.sound}!`);
29+
},
30+
};
31+
32+
// new object
33+
function Dog(name) {
34+
this.name = name;
35+
this.sound = "bark";
36+
37+
// setting prototype using setPrototypeOf()
38+
Object.setPrototypeOf(this, Animal);
39+
}
40+
41+
dog1 = new Dog("Marcus");
42+
43+
console.log(Animal.isPrototypeOf(dog1)); // true
44+
45+
46+
47+
48+
49+
50+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /