//방법1.

function Car(name,color){

this.name = name;

this.color = color;

this.show = function(){console.log(name, color)}

}


var car1 = new Car('morning','blue');

car1.show();



//방법2. 프로토타입 이용


function BigCar(name, color){

this.name = name;

this.color = color;

}


BigCar.prototype.show = function(){

console.log('bigcar' ,this.name , this.color)

}

//자식 객체 1

var bigcar1 = new BigCar('churare','white');

bigcar1.show();

//자식 객체 2

var bigcar2 = new BigCar('dump','black');

bigcar2.show();



//방법3. 객체리터럴(싱글톤)

var smallcar = {

name: "tico",

color: "blue",

show: function(){

console.log('smallcar',this.name,this.color);

}

}

//할당

smallcar.name = "morning2";

smallcar.color = "gold";

smallcar.show();



//*******************************************************js getter setter 1************************************************

var person = {

    firstName: 'Jimmy',

    lastName: 'Smith',

    get fullName() {

        return this.firstName + ' ' + this.lastName;

    },

    set fullName (name) {

        var words = name.toString().split(' ');

        this.firstName = words[0] || '';

        this.lastName = words[1] || '';

    }

}


person.fullName = 'Jack Franklin';

console.log(person.firstName); // Jack

console.log(person.lastName) // Franklin



//*******************************************************js getter setter 2****************************************************

var person2 = {

    firstName: 'kang',

    lastName: 'youngkyuen'

};


Object.defineProperty(person2, 'fullName', {

    get: function() {

        return this.firstName + ' ' + this.lastName;

    },

    set: function(name) {

        var words = name.split(' ');

        this.firstName = words[0] || '';

        this.lastName = words[1] || '';

    }

});

console.log(person2.fullName) 



//********************************************************************************************




//------------------------------------------

function Student(name, age) {

    this.name = name;

     

    this.getName = function() {

        return this.name;

    }

    this.setName = function(name) {

        this.name = name;

    }

}

var preamtree = new Student(name);



//------------------------------------------

function Student(name, age) {

    this.name = name;

}

Student.prototype.getName = function() {

    return this.name;

}

 

Student.prototype.setName = function(name) {

    this.name = name;

}

var preamtree = new Student(name);


//---------------------------------------------------------------------------

//prototype에 프로퍼티를 추가하는 함수

Function.prototype.method = function(name, func) {

    if(!this.prototype[name]) {

        this.prototype[name] = func;

    }

}

 

function Person(name, age) {

    this.name = name;

}

 

// prototype에 프로퍼티를 추가할 때마다 '.prototype'을 반복할 필요가 없음.

Person.method('getName',  function() {

    return this.name;

});

 

Person.method('setName', function(name) {

    this.name = name;

});



//********************************* 상속 *********************************


//부모 클래스

function Car(name,color){

this.name = name;

this.color = color;

this.show = function(){

return this.name + ' ' + this.color;

}


}



function create(o) {

    function F() {}; //빈 자식 클래스

    F.prototype = o;

    return new F(); // o를 부모로하는 객체 리턴

}


var myPcar = new Car('pCar','BLACK');

var sunCar = create(myPcar);

var text = sunCar.show();

console.log(text);



  1. 2018.10.17 16:14

    비밀댓글입니다

//person.js

// person data object.  setter getter 만드는 방법


var firstName, lastName, age;


exports.setFirstName = function (fname) {

    firstName = fname;

};


exports.setLastName = function (lname) {

    lastName = lname;

};


exports.setAge = function (yrsold) {

    age = yrsold;

};


exports.getPersonInfo = function () {

    return {

        firstName: firstName,

        lastName: lastName,

        age: age

    };

};

---------------------------------------------------------------------------------------------------------------


//app.js

var person = require('./person.js');


// steve 라는 이름 할당

person.setFirstName('Steve');


// jobs 라는 이름 할당

person.setLastName('Jobs');


// 56 

person.setAge(56);


console.log(person.getPersonInfo());








//관련 모듈 설치
//npm i nodemailer
//npm i nodemailer-smtp-transport





var nodemailer = require('nodemailer');
//smtp 서버를 사용하기 위한 모듈이다.
var smtpTransport = require('nodemailer-smtp-transport');



//아이디가 존재할 경우
//숫자 + 문자 + 특수문자 새로운 비밀번호 생성하고
var arr = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,~,`,!,@,#,$,%,^,&,*,(,),-,+,|,_,=,\,[,],{,},<,>,?,/,.,;".split(",");
var randomPw = createCode(arr, 10);


//비밀번호 랜덤 함수
function createCode(objArr, iLength) {
var arr = objArr;
var randomStr = "";
for (var j=0; j<iLength; j++) {
randomStr += arr[Math.floor(Math.random()*arr.length)];
}
return randomStr
}




//이메일 발송함수
function sendNewPwFunc(email,pw){

var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: 'thanksman1211@gmail.com',
pass: 'project100$1'
}
}));
var mailOptions = {
from: 'thanksman1211@gmail.com',
to: email,
subject: 'Sending Email using Node.js[nodemailer]',
html:'<h1>00에서 새로운 비밀번호를 보내드립니다.~~~~</h1> <h2>' + pw + '</h2>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
return false;
} else {
console.log('Email sent: ' + info.response);
console.log("새로운 비밀번호가 발송되었습니다.");
return true;
}
});

return true;
}




+ Recent posts

티스토리 툴바