程序员深入理解JavaScript的一些事

02 深入理解function&构造函数

2019-01-08  本文已影响0人  陈柯梦

function&构造函数

演示代码

<html>
    <body>
        <script type="text/javascript">
        "use strict"
            // 通过构造函数 创建类(object)属性
            function fucConstruction(x){
                this.obj = x;
                console.log(`fucConstruction this.obj====>${this.obj}`);
            }
            fucConstruction.prototype.rootobj = "rootobj";
            var a = new fucConstruction('obj');
            console.log(`obj is a property====>${a.hasOwnProperty('obj')}`);
            console.log(`obj is fucConstruction property====>${fucConstruction.prototype.hasOwnProperty('obj')}`);
            console.log(`rootobj is a property====>${a.hasOwnProperty('rootobj')}`);
            console.log(`rootobj is fucConstruction property====>${fucConstruction.prototype.hasOwnProperty('rootobj')}`);
            console.log(`a is ====>${typeof a}`);

            // 函数受返回值影响
            function fucReturnObject(x){
                this.obj = x;
                console.log(`fucReturnObject this.obj====>${this.obj}`);
                return {};
            }
            var b = new fucReturnObject('obj');
            console.log(`obj is b property====>${b.hasOwnProperty('obj')}`);
            console.log(`obj is fucReturnObject property====>${fucReturnObject.prototype.hasOwnProperty('obj')}`);
            console.log(`b is ====>${typeof b}`);

            function fucReturnNull(x){
                this.obj = x;
                console.log(`fucReturnNull this.obj====>${this.obj}`);
                return null;
            }
            var c = new fucReturnNull('obj');
            console.log(`obj is c property====>${c.hasOwnProperty('obj')}`);
            console.log(`obj is fucReturnNull property====>${fucReturnNull.prototype.hasOwnProperty('obj')}`);
            console.log(`c is ====>${typeof c}`);

            function fucReturnFunc(x){
                this.obj = x;
                console.log(`fucReturnFunc this.obj====>${this.obj}`);
                return function(){};
            }
            var d = new fucReturnFunc('obj');
            console.log(`obj is d property====>${d.hasOwnProperty('obj')}`);
            console.log(`obj is fucReturnFunc property====>${fucReturnFunc.prototype.hasOwnProperty('obj')}`);
            console.log(`d is ====>${typeof d}`);

         </script>
    </body>
</html>

通过构造函数 创建类(object)属性

function fucConstruction(x){
    this.obj = x;
    console.log(`fucConstruction this.obj====>${this.obj}`);
}
fucConstruction.prototype.rootobj = "rootobj";
var a = new fucConstruction('obj');
console.log(`obj is a property====>${a.hasOwnProperty('obj')}`);
console.log(`obj is fucConstruction property====>${fucConstruction.prototype.hasOwnProperty('obj')}`);
console.log(`rootobj is a property====>${a.hasOwnProperty('rootobj')}`);
console.log(`rootobj is fucConstruction property====>${fucConstruction.prototype.hasOwnProperty('rootobj')}`);
console.log(`a is ====>${typeof a}`);

构造函数创建的属性属于新类的属性,不被函数原型继承

函数受返回值影响

function fucReturnObject(x){
    this.obj = x;
    console.log(`fucReturnObject this.obj====>${this.obj}`);
    return {};
}
var b = new fucReturnObject('obj');
console.log(`obj is b property====>${b.hasOwnProperty('obj')}`);
console.log(`obj is fucReturnObject property====>${fucReturnObject.prototype.hasOwnProperty('obj')}`);
console.log(`b is ====>${typeof b}`);
function fucReturnNull(x){
    this.obj = x;
    console.log(`fucReturnNull this.obj====>${this.obj}`);
    return null;
}
var c = new fucReturnNull('obj');
console.log(`obj is c property====>${c.hasOwnProperty('obj')}`);
console.log(`obj is fucReturnNull property====>${fucReturnNull.prototype.hasOwnProperty('obj')}`);
console.log(`c is ====>${typeof c}`);
function fucReturnFunc(x){
    this.obj = x;
    console.log(`fucReturnFunc this.obj====>${this.obj}`);
    return function(){};
}
var d = new fucReturnFunc('obj');
console.log(`obj is d property====>${d.hasOwnProperty('obj')}`);
console.log(`obj is fucReturnFunc property====>${fucReturnFunc.prototype.hasOwnProperty('obj')}`);
console.log(`d is ====>${typeof d}`);

函数构造受返回值影响,如果有返回值,则以return结果为准,即"无返回则返回函数构造对象,否则指针后移至新的返回对象"

上一篇 下一篇

猜你喜欢

热点阅读