使用对象 - javscript

// 使用构造函数
function Car(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
}

var mycar = new Car("Eagle", "Talon TSi", 1993);
console.log(mycar);
console.log(typeof mycar);

// 使用Object.create 方法
// Animal properties and method encapsulation
var Animal = {
    type: "Invertebrates", // Default value of properties
    displayType : function() {  // Method which will display type of Animal
      console.log(this.type);
    }
  }
  
  // Create new animal type called animal1 
  var animal1 = Object.create(Animal);
  animal1.displayType(); // Output:Invertebrates
  
  // Create new animal type called Fishes
  var fish = Object.create(Animal);
  fish.type = "Fishes";
  fish.displayType(); // Output:Fishes

Reference:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 14,310评论 7 163
  • 前些日子从@张鑫旭微博处得一份推荐(Front-end-tutorial),号称最全的资源教程-前端涉及的所有知识...
    谷子多阅读 9,805评论 0 44
  • 当我回过神来,知晓自己处在一个什么状况的时候,心中便只剩一个念头,那就是前进。那是我的使命,前进,不顾一切地前进,...
    塞尔希卡斯阅读 4,657评论 0 1
  • 今天想试着写写我身边的同事们,我才发现我从没有很好的观察过她们。于是我开始回忆,哒哒哒……每天早上,只要办公楼里响...
    弈臻阅读 1,495评论 0 0
  • 区块元素段落和换行标题123456# 1## 2### 3#### 4##### 5###### 6区块引用这个是...
    MrHamster阅读 5,536评论 0 0

友情链接更多精彩内容