0% found this document useful (0 votes)
3 views7 pages

Lecture JS - Operator

The document explains the use of the 'new' operator in JavaScript, detailing how it creates a new object, sets its prototype, and executes a constructor function. It also highlights the 'delete' operator, which removes properties from objects but does not affect variables or prototype chain properties. Additionally, it advises against using 'new' with primitive types and compares the 'new' operator with 'Object.create()' in terms of functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Lecture JS - Operator

The document explains the use of the 'new' operator in JavaScript, detailing how it creates a new object, sets its prototype, and executes a constructor function. It also highlights the 'delete' operator, which removes properties from objects but does not affect variables or prototype chain properties. Additionally, it advises against using 'new' with primitive types and compares the 'new' operator with 'Object.create()' in terms of functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Operators

DELETE AND NEW


new
let obj = new ConstructorFunction();
let today = new Date();

function Person(name) {
this.name = name;
}

let john = new Person("John");


console.log(john.name); // John
Creates a blank object.
Sets the prototype of that object to the constructor's prototype.
Executes the constructor function with this pointing to the new object.
Returns the new object (unless constructor explicitly returns another object).
Avoid new with
Primitive types like Number, String, Boolean (use literals instead)

let str = new String("hello"); // avoid


let str = "hello"; // better
Feature new Object.create()
Creates instance from constructor ✅ ❌
Inherits prototype ✅ ✅ (from object, not function)
Simpler prototype-only creation ❌ ✅
delete
Used to remove a property from an object.

delete object.property;

let person = {
name: "Alice",
age: 30
};

delete person.age;
console.log(person); // { name: "Alice" }
Only works on object properties, not variables declared with var, let, or const.
Returns true if the property is successfully deleted or doesn't exist.
Does not affect prototype chain properties.
Deleting array elements creates holes (i.e., leaves undefined values), which can be confusing.

You might also like