OOP In JS [JS脚本中的面向对象]

博客围绕Javascript面向对象编程展开,介绍了在JS中创建类和模块的方法,通过prototype实现继承,如Dog类继承Animal类。还阐述了简化新类的方式,像创建Cat类。此外,提到了数据隐藏,可创建半私有对象,这些方法可用于创建各类输出类和模块。

目标
让我们轻松的实现Javascript的面向对象

我们将了解在JS中如何去创建类和模块。


通过prototype来继承.


假设有两个类: "Animal" and "Dog", Dog 从 Animal 继承过来.
Animal 类如下:

//constructor of Animal
var Animal = function(){

}
//eat method of Animal
Animal.prototype.eat = function(food){
    //process the food here
}

创建实例并使用其方法:

//create an Animal object
myAnimal = new Animal();
//let the animal eat
myAnimal.eat("yummy food");


Dog 类看起来也非常相似。
我们仅仅来通知 Dog 它是继承于 Animal 就可以了:

//constructor for Dog
var Dog = function(){

}
//"inheriting" from Animal
Dog.prototype = new Animal();
//resetting the constructor to Dog
Dog.prototype.constructor = Dog;

//bark method of Dog
Dog.prototype.bark = function(){
    //make some noise
}

现在我们来创建一个 Dog 类,它具有 bark 和 eat 方法:

//create a new Dog object
var myDog = new Dog();
//let it bark
myDog.bark();
//throw it a bone
myDog.eat("yummy bone");


可以看出 Dog 继承了 Aninal 类的 "eat" 方法.
Let us assume the dog needs to first pre process the food(chew the bone) and then have it processed by the super class Animal.
We simply overwrite the "eat" method and call the super class' "eat" method.

Dog.prototype.eat = function(food){
    //preprocess the food here
    
    //call the super class' eat method with the dog as the this object
    Animal.prototype.eat.call(this, food);
}

Now we have a dog how chews the bone:


//创建一个新的Dog对象
var myDog = new Dog();
//使用eat方法
myDog.eat("yummy bone");


让它更加简单.
As you have noticed above there is a lot of typing involved in setting up a subclass:

写一个构造器
追加它的属性或方法
在继承过来的新类中重载它的属性或方法.
Every time the class is used for prototyping its constructor is called.
但这也可能不是我们想要的效果.

让我们来简化这个新类.
做到让 Cat 类像如下所示:

//从Animal中扩展出一个新类 "Cat".
var Cat=Class("Cat", Animal, function(thisClass, Super){
    //called when Cat is being instaciated
    thisClass.prototype.init = function(){
        //在这里初始化
        alert("Cat.prototype.init()")
    }
    
    thisClass.prototype.eat = function(food){
        //处理如何消化食物
    }
})

这样一来 Cat类就可以像其他普通类一样的使用了.


Class function
The Class function:
设计一个普通的类构造器
继承于父级类;
it's property className is set;
a default toString method is created for convinience;
and the classScope function is run with the class and the super class as parameters.
When an object is created from the new class the init method is called with all parameters passed to the constructor.
When a prototype for subclassing is created from a class, the init method is not called.
This makes init act just like any other constructor function in JavaScript.


//create a new class. The init method of cat should not be called.
var GreenCat = Class("GreenCat", Cat,  function(){});
alert("GreenCat created/nCat.prototype.init() was not called.");
//create an object of GreenCat
var myGreenCat = new GreenCat();
alert("myGreenCat created.");


数据隐藏
If you take a closer look at the Cat above you notice, that there is a function being passed to Class.
All variables declared with var withing this function are private to this function.
We can now create a class which can use some semi private objects only visible to the class itself.

var SomeClass=Class("SomeClass", function(thisClass, Super){
    
    //function only visible within this class
    var someFunction = function(x){
        return 2 * x;
    }
    
    //a method which calls the "private" function
    thisClass.prototype.someMethod=function(arg){
        return someFunction(arg);
    }
})
使用方式如下:
//create an object of SomeClass
var obj = new SomeClass();
//try the method the return value should be 6
alert(obj.someMethod(3))


同样的方法可以用来创建拥有子类、函数和其他方法属性的输出类和模块.


 

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值