JSFiddle - React, Tailwind, and code Playground

by ChangJoo Park

JavaScript

function HelloFunc(func) {
	this.greeting = 'hello'
}

HelloFunc.prototype.call = function (func) {
func ? func(this.greeting) : this.func(this.greeting)
}

var userFunc = function (greeting) {
	console.log(greeting)
}

var objHello = new HelloFunc()
objHello.func = userFunc;

/* userFunc를 사용 */
console.log('---')
objHello.call();

console.log('---')
/* 새 함수를 전달 */
objHello.call((greeting) => { 
	console.log('안녕하세요')
});


class NewHello {
  constructor(greeting) {
    this.greeting = greeting ? greeting : 'hello';
  }
  
  func () {
  	console.log(this.greeting)
  }
}


var newHelloInstance = new NewHello();
var anotherHelloInstance = new NewHello('안녕하세요');

newHelloInstance.func()
anotherHelloInstance.func()