public class ProductListViewComponent : ViewComponent
{
private readonly IProductRepository _productRepository;
public ProductListViewComponent(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public async Task<IViewComponentResult> InvokeAsync(int categoryId)
{
var products = await _productRepository.GetProductsByCategoryAsync(categoryId);
return View(products);
}
}
<vc:product-list category-id="1" />
@await Component.InvokeAsync("ProductList", new { categoryId = 1 })
SELECT CONVERT(varchar, GETDATE(), 23);
SELECT DATEADD(day, 30, '2023-04-26');
SELECT SWITCHOFFSET(CONVERT(datetimeoffset, '2023-04-26 12:00:00', 120), '+00:00');
SELECT DATEDIFF(day, '2023-04-26', '2023-05-01');
This will return the number of days between the two dates.// Function Declaration
function greet(name) {
console.log("Hello, " + name);
}
// Function Expression
const greet = function(name) {
console.log("Hello, " + name);
}
C. Function Parametersfunction greet(name) {
console.log("Hello, " + name);
}
greet("John"); // Output: Hello, John
D. Function Return Valuesfunction square(num) {
return num * num;
}
const result = square(4);
console.log(result); // Output: 16
III. Creating Functions in JavaScriptfunction greet(name) {
console.log("Hello, " + name);
}
const greet = function(name) {
console.log("Hello, " + name);
}
const greet = (name) => {
console.log("Hello, " + name);
}
const numbers = [1, 2, 3, 4];
numbers.forEach(function(num) {
console.log(num);
});
// Output:
// 1
// 2
// 3
// 4
let name = "John";
function greet() {
console.log("Hello, " + name);
}
greet(); // Output: Hello, John
function greet(name) {
let message = "Hello, " + name;
console.log(message);
}
greet("John"); // Output: Hello, John
console.log(message); // Uncaught ReferenceError: message is not defined
if (true) {
let message = "Hello, John";
console.log(message); // Output: Hello, John
}
console.log(message); // Uncaught ReferenceError: message is not defined
Array.prototype.map()
method, which takes a callback function as an argument and returns a new array with the results of the callback function applied to each element.const numbers = [1, 2, 3, 4];
const doubledNumbers = numbers.map(function(num) {
return num * 2;
});
console.log(doubledNumbers); // Output: [2, 4, 6, 8]
function outerFunction() {
let message = "Hello, John";
function innerFunction() {
console.log(message);
}
return innerFunction;
}
const closure = outerFunction();
closure(); // Output: Hello, John
function createCounter() {
let count = 0;
return {
increment: function() {
count++;
},
getCount: function() {
return count;
}
};
}
const counter = createCounter();
counter.increment();
counter.increment();
console.log(counter.getCount()); // Output: 2
function factorial(num) {
if (num === 1) {
return 1;
}
return num * factorial(num - 1);
}
console.log(factorial(5)); // Output: 120
VIII. Callbacks in JavaScriptsetTimeout()
function, which takes a callback function and a delay time as arguments and executes the callback function after the specified time has passed.setTimeout(function() {
console.log("Hello, John");
}, 1000); // Output: Hello, John (after 1 second)
call()
, apply()
, and bind()
methods.call()
method.const person = {
name: "John",
greet: function() {
console.log("Hello, " + this.name);
}
};
person.greet(); // Output: Hello, John
const anotherPerson = {
name: "Jane"
};
person.greet.call(anotherPerson); // Output: Hello, Jane
.call()
method allows you to invoke a function and set the value of "this" within that function..apply()
method works similarly to the .call()
method, but it accepts an array of arguments instead of separate arguments..bind()
method creates a new function with the value of "this" set to the specified value.