js面向对象基础概念

写在前面: 本文针对ES6规则前的js, ES6后 由static class等关键字 js日趋完善.

公有属性和方法 (公有方法包括 特权方法和原型方法)

function Test(sex, age) {
    this.sex = sex;   //公有属性
    this.age = age;
    this.temp = 'temp';
}
// ❗️并不推荐在构造函数中 添加公有方法 (也被称作特权方法) 因为实例化后每个实例的同名特权方法都要重新创建
Test.prototype.getSex = function () {  //公有方法
    return this.sex;
}

var test = new Test('sale', 29);
console.log(test.getSex());     //  output: sale
console.log(test.sex);          //  output: sale
console.log(test.age);          //  output: 29
console.log(test.temp);         //  output: temp 
console.log(Test.temp);         //  output: undefined

私有属性和方法

function Test(sex, age) {
    var sex = sex;  //私有属性
    var age = age;
    function consoleAge() {  //私有方法
        console.log('age', age);
    }
    // ❗️私有方法只能在 函数作用域内部使用 
    consoleAge(); // output: 29
}
var test = new Test('male', 29);

静态属性和方法——无需实例化就可以调用的方法就叫做静态方法, 即new操作符实例化对象, 用字面量的方式来创建

var test = {
    init: function (sex, age) {
        this.sex = sex;
        this.age = age;
    },
    getSex: function () {
        return this.sex;
    }
}
test.init('male', 29);
console.log(test.getSex());   //output: male

特权方法(也是构造函数内的公有方法, 比较特殊)

function Test(sex, age) {
    var sex = sex;//私有属性
    var age = age;
    this.getSex = function () { //特权方法
        return sex; //私有属性和方法不能使用this调用
    }
}
var test = new Test('male', 29);
console.log(test.getSex());//output: male

公有方法的调用规则——必须先实例化对象
公有方法中通过this调用公有属性和特权方法;
不能使用this调用静态方法和属性,必须通过对象本身调用,即对象名;
公有方法也不能调用私有方法(特权方法可以调用私有方法)

function Test() {
    this.sex = 'male';//公有属性
    this.age = 29;
    this.do = function () {//特权方法
        return `性别是:${this.sex}`;
    }
}
Test.learn = function (title) {
    return `今夜学习${title}`
}
Test.prototype.consoleAge = function () {
    console.log(this.age);
}
Test.prototype.consoleDo = function () {
    console.log(this.do()); //调用特权方法
}
Test.prototype.consoleLearn = function (title) {
    console.log(Test.learn(title));    //只能通过对象本身调用静态方法
    //console.log(this.learn(title))   这样调用将出错:this.learn is not a function
}
var test = new Test();
test.consoleAge();    //  output: 29
test.consoleDo();     //  output: 性别是male
test.consoleLearn('js');   

静态方法的调用规则——无需实例化对象, 实例对象不能调用对象的静态方法, 只能调用实例自身的静态属性和方法
静态方法无法调用公有属性、公有方法、私有方法、私有属性、特权方法和原型属性

function Test() {
    this.sex = 'male';  //公有属性
    this.age = 29;
    this.do = function () {   //特权方法
        return `他已经${this.age}岁了`;
    }
}
Test.prototype.consoleAge = function () {   //公有方法,也叫原型方法
    console.log(this.age);
}
Test.prototype.address = 'bj';      //原型属性
Test.getSex = function () {    //静态方法
    return this.sex;
}
Test.getAge = function () {
    this.consoleAge();
}
Test.getDo = function () {
    return this.do();
}
// console.log(Test.getSex())    // undefined
//console.log(Test.getDo());    // ❌ TypeError: this.do is not a function
//console.log(Test.getAge());   // ❌ TypeError: this.consoleAge is not a function

特权方法调用规则——通过this调用公有方法和公有属性,通过对象本身调用静态方法和静态属性,在方法体内直接调用私有方法和私有属性

function Test(_arg) {
    var arg = _arg;
    function getArg() {
        return `获取的参数是${arg}`;
    }
    this.sex = 'male';//公有属性
    this.age = 29;
    this.do = function () { //特权方法
        return `这是一位${this.sex}`;
    }
    this.consoleAge = function () {
        this.addAge();   //特权方法调用公有方法
        console.log(this.age);
    }
    this.consoleArg = function () {
        console.log(getArg());  //调用私有方法
    }
}
Test.prototype.addAge = function () {
    this.age++;
}
var test = new Test('javascript');
test.consoleAge();  //  output: 30
test.consoleArg();  //  output: 获取的参数是javascript

私有方法
对象的私有方法和属性,外部是不可以访问的,在方法的内部是不能this调用对象的公有方法、公有属性、特权方法的

function Test(_arg) {
    var arg = _arg;
    this.sex = 'male';//公有属性
    this.age = 29;
    function innerFunc() {
        console.log(Test.eat('泡面'));// output : 晚餐只有泡面
        console.log('arg', arg)
    }
    this.func = function () {
        innerFunc();//调用私有方法
    }
}
Test.eat = function (supper) {
    return '晚餐只有' + supper;
}
var test = new Test('某某');
test.func();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容