javascirpt

3297 단어 javascirpt
var Anim = function() {
};
Anim.prototype.start = function() {
console.log('1');
};
Anim.prototype.stop = function() {
console.log('2');
};
var myAnim = new Anim();
myAnim.start();
myAnim.stop();
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype = {
getName: function() {
return this.name;
},
getAge: function() {
return this.age;
}
}
/* Instantiate the class. */
var alice = new Person('Alice', 93);
var bill = new Person('Bill', 30);

console.log(bill.getAge());
var Student = function(){
    this.username = "xiewnbo";
    this.number = "1321321321";
}

Student.prototype= {
    sayhello:function(){
        console.log(this.username);
    },
    saybye:function(){
        console.log(this.number);
    }
};

var std = new Student();
std.sayhello();
// Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length +
"arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
// Static class method.
Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " +
arguments.length + "arguments, but expected at least 2.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments"
+ "two and above to be instances of Interface.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name
+ " interface. Method " + method + " was not found.");
}
}
}
};

    var IActionListener = new Interface("IActionListener",["method1","method2"]);        
    // 
    var oActionListener = {
        method1 : function(){
            console.log(" 1");
        },
        method2 : function(){
            console.log(" 2");
        }
    };
    //implements 
    Interface.ensureImplements(oActionListener,IActionListener);
    // 
    oActionListener.method1();

좋은 웹페이지 즐겨찾기