Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions submissions/Ghaida-Jaaisa/assignment8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Task 1
const editor = {
title: "my first blog",
getUpperTitle() {
return this.title.toUpperCase();
},
};
const getTitle = editor.getUpperTitle.bind(editor);
console.log(getTitle());

// Task 2
const formHandler = {
value: "initial",
onChange(newValue) {
this.value = newValue;
},
};
const simulateInputChange = (callback) => {
callback("updated");
};
simulateInputChange(formHandler.onChange.bind(formHandler));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use arrow function as another solution here and explain why it could work, and please explain why u use bind here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the arrow function?

// Why do we need to bind here?
// We need to bind here to ensure that 'this' inside onChange refers to formHandler,
console.log(formHandler.value); // Excpected output: 'updated'

/*
another solution
use arrow function
simulateInputChange((value) => formHandler.onChange(value));
*/

// Task 3
const translator = {
language: "Arabic",
getLanguage: () => `Current language: ${translator.language}`,
};

function logLanguageInfo(getter) {
console.log(getter.call(translator));
// Using call to ensure 'this' refers to translator
}

logLanguageInfo(translator.getLanguage);