JS大杂烩

$(function(){
    
    //alert($.add(1, 5));    //6
    
    //alert($.text); //hello
    
    //alert($.fn.add(1, 5));    //6
    
    //alert($.fn.text);    //hello
    
    
    var myFun = new MyFun(1, 2);
    
    //alert(myFun.firNum);    //1
    
    //alert(myFun.add(1,5));    //6
});



$.extend({
    add : function (a, b) {
        return a + b;
    },
    text : "hello"
});

$.fn.extend({
    add : function (a, b) {
        return a + b;
    },
    text : "hello"
});

function MyFun(fir, sec) {
    this.firNum = fir;
    this.secNum = sec;
}

MyFun.prototype.add = function(a, b) {
    return a + b;
}

 

$(function(){
    
    //1
    //用 add 来替换 sub
    //var res = add.apply(sub, [5, 2]);        //7
    //var res = sub.apply(null, [5, 2]);    //3
    
    //var res = add.call(null, 5, 2);    //7
    //var res = sub.call(null, 5, 2);    //3
    
    alert(res);
    
    //2
    var f1 = new fun1();
    var f2 = new fun2();
    //alert(f1.add.call(f2));    //333
    //alert(f1.add.apply(f2));    //333
    
    //3
    var c2 = new class2();
    //alert(c2.showTxt("hello javaScript"));    //hello javaScript
    
    //4
    var f4 = new fun4_3();
    //alert(f4.showAdd(5, 2));    //7
    //alert(f4.showSub(5, 2));    //3
    
    //5
    var arr1 = [1, 2, 3];
    var arr2 = [4, 5, 6];
    //Array.prototype.push.apply(arr1, arr2);
    //Array.prototype.push.call(arr1, arr2);
    Array.prototype.push.call(arr1, 4, 5, 6, 7);
    //alert(arr1);    //1,2,3,4,5,6,7
});

//1
function add (a, b) {
    return a + b;
}

//1
function sub (a, b) {
    return a - b;
}

//2
function fun1 () {
    this.a = 123;
    this.b = 456;
    this.add = function () {
        return this.a + this.b;
    }
}

//2
function fun2 () {
    this.a = 222;
    this.b = 111
}

//3
function class1 () {
    this.showTxt = function (txt) {
        return txt;
    }
}

//3
function class2() {
    class1.call(this);
}

//4
function fun4_1 () {
    this.showAdd = function (a, b) {
        return a + b;
    }
}

//4
function fun4_2 () {
    this.showSub = function (a, b) {
        return a - b;
    }
}

//4
function fun4_3 () {
    //fun4_1.call(this);
    //fun4_2.call(this);
    
    fun4_1.apply(this);
    fun4_2.apply(this);
}

 

 

你可能感兴趣的:(JS大杂烩)