JavaScript Apply Function
Javascript Bind Function இங்கு ஒரு function இன் this context ஐ மாற்றுகிறது நாம் argument ஆக அனுப்பும் object ஐ current object ஆக எடுத்து கொண்டு அதற்கு ஏற்றவாறு function ஐ execute செய்கிறது இங்கு முக்கியமாக bind function ஆனது ஒரு புதிய function ஐ தருகிறது நாம் இதை ஒரு variable இல் store செய்து கொண்டு பிறகு variable call செய்து கொள்ளலாம்
JavaScript Apply Function
Javascript Apply Function இங்கு ஒரு function இன் this context ஐ மாற்றுகிறது நாம் argument ஆக அனுப்பும் object ஐ current object ஆக எடுத்து கொண்டு அதற்கு ஏற்றவாறு function ஐ execute செய்கிறது.
function_name.apply($arguments);
Example1
<script>
function greet(greeting) {
return `${greeting}, I am ${this.name} and I am ${this.age} years old`;
}
const data = {
name: 'abc',
age: 24,
};
document.writeln(greet.apply(data, ['Hi']));
</script>
மேலே உள்ள Example1-ஐ கவனிக்கவும் இங்கு greet என்ற function current object இல் இருக்கும் name மற்றும் age என்ற key இன் value ஐ display செய்கிறது. apply method ஆனது object இன் this context ஐ மாற்றுகிறது. இங்கு data என்ற object க்கு name மற்றும் age என்ற key values கள் உள்ளன. இங்கு greet.apply(data, ['Hi']) என்ற முறையில் call செய்யும் போது greet function இன் this context இல் data என்ற object கிடைக்கிறது, எனவே இங்கு output "Hi, I am abc and I am 24 years old" என கிடைக்கிறது.
Hi, I am abc and I am 24 years old
Example2
<script>
function display(data) {
return `${data}, Please Visit ${this.name}`;
}
const channel = {
name: 'Parallel Codes'
};
document.writeln(display.apply(channel, ['Learn Programming In Tamil']));
</script>
மேலே உள்ள Example2-ஐ கவனிக்கவும் இங்கு display என்ற function current object இல் இருக்கும் name என்ற key இன் value ஐ display செய்கிறது. Apply method ஆனது object இன் this context ஐ மாற்றுகிறது. இங்கு channel என்ற object க்கு name: 'Parallel Codes' என்ற key values கள் உள்ளன. இங்கு display.apply(channel, ['Learn Programming In Tamil']) என்ற முறையில் call செய்யும் நமக்கு output ஆனது Learn Programming In Tamil, Please Visit Parallel Codes என கிடைக்கிறது.
Learn Programming In Tamil, Please Visit Parallel Codes
will you upload angular?