2013-11-09 3 views
0

jQuery.Color 개체를 반환하는 함수를 만들 필요가 있고 그것을 수행하는 방법을 모른다. 다음 코드 예제에게 있습니다Jquery 생성자를 자체 함수에서 실행하는 방법은 무엇입니까?

function newcolor() { var obj = new $.Color('rgb(0,0,0)'); return obj;} 

var foo = newcolor(); 

foo.red(); 

편집 :

내 전체 코드 :

function my (element,a,b,c){ //class my 
    this.target = $(elem); 
    this.new_color = function (a,b,c) { return new $.Color('rgb('+a+','+b+','+c+')'); } 
    this.base_color = new_color (a,b,c); 
    this.colorize = function() (this.target.css({ background-color: new_color }); 
} 

var div = new My($('foo'),0,0,0); 
div.new_color(255,255,255); 
div.colorize(); 

내 목표는 JQuery와 요소를 보유하고 작동 할 수 클래스를 만드는 것입니다. 이제 $ .Color()를 반환하는 데 막혔습니다.

답변

1

어떻게 이런 일에 대해 :

function My(elem,r,g,b){ //class my 

    this.setColor = function(r,g,b) { 
    this.r = (r || 0); 
    this.g = (g || 0); 
    this.b = (b || 0); 
    }; 

    this.colorArray = function() { 
    return [this.r, this.g, this.b]; 
    }; 

    this.colorString = function() { 
    return "rgb(" + this.colorArray().join(",") + ")"; 
    }; 

    this.colorize = function(r,g,b) { 
    if (r && g && b) { 
     this.setColor(r,g,b); 
    } 
    var color = $.Color(this.colorString()); 
    this.target.css({backgroundColor: color}); 
    } 

    // initialize 
    this.target = $(elem); 
    this.setColor(r,g,b); 
    this.colorize(); 
}; 

var div1 = new My($('#div1'),100,20,40); 
div1.setColor(255,255,200); 
div1.colorize(); 

var div2 = new My($('#div2'),100,20,40); 

당신은 내가 기본적으로 일부 래퍼 함수를 ​​추가하고 단지 인스턴스에 대한 별도의 R, G, B 값을 저장 한 것을 알 수 있습니다. 그런 다음 필요한 순간에 j1Query.Color 메서드 만 호출합니다. 그렇다면 컬러 인스턴스를 놓을 필요가 없습니다.

나뿐만 아니라 codepen에 넣어했습니다 http://codepen.io/bunnymatic/pen/yLxwp

+0

는 당신은 당신이 setColor를 호출 그 때마다이 색이 갱신되도록 colorize''setColor' 실행'함으로써 그것을 더 걸릴 수 있습니다. –

+0

도움과 시간을 보내 주셔서 감사합니다.이 코드를 구현하여 작동 방식을 확인하겠습니다. – Invidian

관련 문제