console.log(x); // undefined
var x = 10;
// 相當於
var x;
console.log(x);
x = 10;
// C# 中
Console.WriteLine(x); // 編譯錯誤
int x = 10;
console.log('5' == 5); // true
console.log('' == 0); // true
console.log(true == 1); // true
// C# 中
// if ("5" == 5) // 編譯錯誤
// 需要明確轉換
if (int.Parse("5") == 5) // 正確
function add(a, b) {
return a + b;
}
const multiply = function(a, b) {
return a * b;
};
const subtract = (a, b) => a - b;
const person = {
name: 'John',
age: 30,
greet() {
return `Hello, I'm ${this.name}`;
}
};
// C# 必須先定義類別
var person = new Person {
Name = "John",
Age = 30
};
person.location = 'Taipei';
delete person.age;
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, I'm ${this.name}`;
}
}
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age) {
Name = name;
Age = age;
}
public string Greet() {
return $"Hello, I'm {Name}";
}
}