Skip to content

Latest commit

 

History

History
134 lines (118 loc) · 3.13 KB

前端-第4周第1次-JS(2)-2022·2·7.md

File metadata and controls

134 lines (118 loc) · 3.13 KB
1
// 构造函数写法
	function Circle(radius, x, y) {
            const pi = 3.1415927;

            this.radius = radius || 1;
            this.x = x ? x : 0;
            this.y = y ? y : 0;

            this.getRadius = function () {
                return this.radius;
            };
            this.setRadius = function (radius) {
                this.radius = radius;
            };

            this.getPerimeter = function () {
                return this.radius * pi * 2;
            };

            this.getArea = function () {
                return pi * Math.pow(this.radius, 2);
            };

            this.getDistance = function (circle) {
                try {
                    if(typeof circle !== 'object' || !(circle instanceof Circle)) {
                        throw 'parameter is not a Circle instance';
                    } else {
                        return Math.sqrt(Math.pow(circle.x - this.x, 2) + Math.pow(circle.y - this.y, 2));
                    }
                } catch (e) {
                    console.log(e);
                }
            };
        };

// class 写法
	class Circle {
        pi = 3.1415927;

        constructor (radius, x, y) {
            this.radius = radius ?? 0;
            this.x = x ?? 0;
            this.y = y ?? 0;
        };

        getRadius () {
            return this.x;
        };
        setRadius (radius) {
            this.radius = radius;
        };

        getPerimeter () {
            return this.radius * this.pi * 2;
        };
        getArea () {
            return this.pi * Math.pow(this.radius, 2);
        };
        getDistance (circle) {
            try {
                if(!(circle instanceof Circle)) {
                    throw 'parameter is not a Circle instance';
                } else {
                    return Math.sqrt(Math.pow(circle.x - this.x, 2) + Math.pow(circle.y - this.y, 2));
                }
            } catch (e) {
                console.log(e);
            }
        };

        static isCircle (circle) {
            return circle instanceof Circle;
        };
    }
2
	(1)
		array.includes(245)
		array.indexOf(245) !== -1
		array.lastIndexOf(245) !== -1
		array.find(function (v) {
		    return v === 245;
		}) !== undefined
		array.findIndex(function (v) {
		    return v === 245;
		}) !== -1
		array.filter(function (v) {
		    return v === 245;
		}).length !== 0
		array.some(function (v) {
		    return v === 245;
		})
		!array.every(function (v) {
		    return v !== 245;
		})
	(2)
		array.indexOf(245)
		array.findIndex(function (v) {
		    return v === 245;
		})
	(3)
		array.map(function (v) {
		    return v.id;
		})
	(4)
		array.filter(function (v) {
		    return v < 200;
		})
	(5)
		array.reduce((pre, cur) => pre + cur, 0)
	(6)
		array.map(function (v) {
		    return ++v;
		})
3
	(1)
		str.indexOf(sonstr);
		str.search(sonstr);
		str.match(sonstr).index;
	(2)
		str.split('?')[1].split('&').forEach(v => v.split('=')[0] === param ? res = v.split('=')[1] : '');