Servicenow class,object.prototypeの使い方のサンプル

クラスの定義
var クラス名 = Class.create();
クラス名.prototype = {
initialize: function(){処理コード}
関数名: function(変数名){処理コード}
}
var 変数名 = new クラス名();
変数名.関数名(値);

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var student = Class.create();
student.prototype = {
initialize: function() {
this.firstName = '';
this.lastName = '';
},
setFirstName : function(str) {
this.firstName = str;
},
setLastName : function(str) {
this.lastName = str;
},
getDisplayName : function() {
return this.firstName + ' ' + this.lastName;
},
type: student
};
var cft = new student();
cft.setFirstName('Yamada');
cft.setLastName('Oosaki')
gs.info('cft=' + cft.firstName + ' ' + cft.lastName);
var name = cft.getDisplayName();
gs.info(name);
var student = Class.create(); student.prototype = { initialize: function() { this.firstName = ''; this.lastName = ''; }, setFirstName : function(str) { this.firstName = str; }, setLastName : function(str) { this.lastName = str; }, getDisplayName : function() { return this.firstName + ' ' + this.lastName; }, type: student }; var cft = new student(); cft.setFirstName('Yamada'); cft.setLastName('Oosaki') gs.info('cft=' + cft.firstName + ' ' + cft.lastName); var name = cft.getDisplayName(); gs.info(name);
var student = Class.create();
student.prototype = {
    initialize: function() {
      this.firstName = '';
      this.lastName  = '';
    },

    setFirstName : function(str) {
      this.firstName = str;
    },

    setLastName : function(str) {
      this.lastName = str;
    },

    getDisplayName : function() {
      return this.firstName + ' ' + this.lastName;
    },

    type: student
};

var cft = new student();
cft.setFirstName('Yamada');
cft.setLastName('Oosaki')
gs.info('cft=' + cft.firstName + ' ' + cft.lastName); 
var name = cft.getDisplayName();
gs.info(name);

 

IT

Posted by arkgame