javascript - 面向对象


以下代码为变量 car 设置值为 "Fiat" :

var car = "Fiat";

对象也是一个变量,但对象可以包含多个值(多个变量)。

var car = {type:"Fiat", model:500, color:"white"};

在以上实例中,3 个值 ("Fiat", 500, "white") 赋予变量 car。

在以上实例中,3 个变量 (type, model, color) 赋予变量 car。

Note JavaScript 对象是变量的容器

 prototype 属性#


定义和用法#

prototype 属性允许您向对象添加属性和方法

注意: Prototype 是全局属性,适用于所有的Javascript对象。

#

语法#

object.prototype.name=value

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script>
        function employee(name, jobtitle, born) {
            this.name = name;
            this.jobtitle = jobtitle;
            this.born = born;
        }
        var fred = new employee("Fred Flintstone", "Caveman", 1970);
 
        employee.prototype.salary = null;
 
        fred.salary = 20000;
 
        document.write(fred.salary);
    </script>

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function Person(name) {
    var _this = {};
    _this._name = name;
 
    _this.sayHello = function() {
        alert("P_hello" + _this._name);
    }
    return _this;
}
 
function Teacher(name) {
    // body...
    var _this = Person(name);
    var superSay = _this.sayHello;
    _this.sayHello = function(argument) {
        superSay.call(_this);
        alert("T_hello" + _this._name);
    }
    return _this;
}
 
var t = Teacher("aaaa");
//console.log(t);
t.sayHello();

 


 

作者:【唐】三三

出处:https://www.cnblogs.com/tangge/p/5708128.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   【唐】三三  阅读(194)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示