2018년 2월 10일 토요일

JAVASCRIPT 유용한 함수들 -- 잘까먹거나 안썼던 것들

-Array.every(function(value){
  return conditional;
}); 
 : 모든 항목이 true 여야만 결과가 true 가된다.조건의 하나라도 실패하면 실패가된다.
-->ex
var array = [1, 3, 5, 7, 9];
array.every(function(i) {
  return i % 2 === 1;
}); // true
array.every(function(i) {
  return i < 9;
}); // false

-Array.some(function(value){
  return conditional;
}); 
 : 한개의 항목이라도 조건에서 true 면 성공이다. 마치 sql 의 in_array 비슷한기능
-->ex
array.some(function(i) {
  return i === 9;
}); // true

-Array.isArray(값)
 : 값이 배열인지 확인, 값이 없으면 에러..
-->ex
Array.isArray(array1)// true


-함수안에 객체함수를 만드는 것보다 prototype 을 만드는게 메모리에 효과적이다. 함수안의 객체는 new 로 새로 만들떄마다 각 함수마다 객체함수를 들고다니지만, prototype 은 함수자체에 포함되어 메모리를 아낄수 있다.

function MyMethod(arg1, arg2){
   this.arg1 = arg1
   this.arg2 = arg2
   this.innerMethod = function() {
      return this.arg1 + this.arg2;
   }
}
보다는

function MyMethod(arg1, arg2){
   this.arg1 = arg1
   this.arg2 = arg2
}
MyMethod.prototype.innerMethod= function() {

      return this.arg1 + this.arg2;
  }


-JAVASCRIPT 의 상속방법은 Object.create.
단일상속
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

멀티상속
mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/create

0 comments:

댓글 쓰기