Category Archives: Javascript

var array = [{id: 1, name: "yyy"},{id: 2, name: "mmmm"}]
for (var i in array){
  console.log(array[i].id)
  console.log(array[i].name)
}

js コンストラクタでselect object作成

$(function(){
  var s = new Select({1:"hello1", 2:"hello2"}, changeFunc);
  s.append();
});

function Select(values, changeFunc){
  this.object = $('<select>').append($('<option>').val(0))
  for (k in values){
    this.object.append($('<option>').val(k).text(values[k]))
  }
  this.object.bind("change", changeFunc)
  this.append = function(){$('div').append(this.object)}
  $('label').text("hi")
}

function changeFunc(event){alert($(this).val())}

js buttonをコンストラクタで生成

$(function(){
  var b = new Button("yohei",clickFunc)
  b.append()
});

function clickFunc(event){alert(event.data.name)}

function Button(name, clickFunc){
  this.name = name
  this.object = $('<button>').text("button").bind("click",{name:this.name},clickFunc);
  this.append = function(){$('div').append(this.object)}
}