재우니의 블로그



public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string GetFullName()
    {
        return FirstName + " " + LastName;
    }
} 

var p = new Person { FirstName = "Jon", LastName = "Smith" };
Console.WriteLine(p.GetFullName()); 


클래스에 속성을 기재하고, 이를 인스턴스하여 값을 할당한 다음,  getfullname 메소드를 호출한다.



function Person(fn,ln)
{
    this.FirstName = fn;
    this.LastName = ln;
    this.GetFullName = function()
    {
        return this.FirstName + ' ' + this.LastName;
    }
}
 
var p = new Person('jon','smith');
alert(p.GetFullName());  


위는 javascript 를 이용한 구문이다. 차이점은 function 에 파라미터 값을 전송한다.



unction Person(fn,ln)
{
    
    this.FirstName = fn;
    this.LastName = ln;
    this.GetFullName = function()
    {
        return this.FirstName + ' ' + this.LastName;
    }
} 
Person.prototype= {};  


아래는 prototype 으로 함수를 별도로 기재하여 처리한다. 


function Person(fn,ln)
{
    this.FirstName = fn;
    this.LastName = ln;
}

Person.prototype.GetFullName = function()
{
    return this.FirstName + ' ' + this.LastName;
}
 
var p = new Person('jon','smith');
alert(p.GetFullName()); 

또 다른 방법은????


unction Person(fn,ln)
{
    this.FirstName = fn;
    this.LastName = ln;
    if(typeof this.GetFullName != "function"){
        Person.prototype.GetFullName = function()
        {
            return this.FirstName + ' ' + this.LastName;
        }
    }
}
var p = new Person('jon','smith');
alert(p.GetFullName());