首页 > 编程语言 >javascript的一些基础知识

javascript的一些基础知识

时间:2023-02-17 13:34:17浏览次数:47  
标签:function javascript object 基础知识 Object objects new 一些 method

随手记录一些javascript的一些基础知识,之前只是简单用到javascript,并没有了解其中的概念。

1.  Javascript Object:

In JavaScript, almost "everything" is an object.

  • Booleans can be objects (if defined with the new keyword)
  • Numbers can be objects (if defined with the new keyword)
  • Strings can be objects (if defined with the new keyword)
  • Dates are always objects
  • Maths are always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are always objects

All JavaScript values, except primitives, are objects.

1.1 Object Methods

Methods are actions that can be performed on objects.

Object properties can be both primitive values, other objects, and functions.

An object method is an object property containing a function definition.

PropertyValue
firstName John
lastName Doe
age 50
eyeColor blue
fullName function() {return this.firstName + " " + this.lastName;}
  JavaScript objects are containers for named values, called properties and methods.    1.2 创建object的方式:常用的create object有两种方式: a) with new keyword const person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";   b) Using an Object Literal const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};  

1.3 JavaScript Object Methods

JavaScript methods are actions that can be performed on objects. 注意这里的用词是method,而不是function。

A JavaScript method is a property containing a function definition.

PropertyValue
firstName John
lastName Doe
age 50
eyeColor blue
fullName function() {return this.firstName + " " + this.lastName;}
  可以用系统的build-in method: 例如: let message = "Hello world!";
let x = message.toUpperCase();   1.4  Object Constructors

Sometimes we need a "blueprint" for creating many objects of the same "type".

The way to create an "object type", is to use an object constructor function.

In the example above, function Person() is an object constructor function.

Objects of the same type are created by calling the constructor function with the new keyword:

const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");

我的理解:object constructors其实就是function,用typeof打印出来也是function。 A function defined as the property of an object, is called a method to the object.
A function designed to create new objects, is called an object constructor.   1.5  Object Prototypes 对于上面的Persion这个constructor function,you can not add a new property to an existing object constructor,例如这样是错误的:Person.nationality = "English"; 但你可以在实例化的object中添加一个property,如果你需要对这个模版的函数统一增加一个属性,可以用prototypes:

All JavaScript objects inherit properties and methods from a prototype:

  • Date objects inherit from Date.prototype
  • Array objects inherit from Array.prototype
  • Person objects inherit from Person.prototype

The Object.prototype is on the top of the prototype inheritance chain:

Date objects, Array objects, and Person objects inherit from Object.prototype.

The JavaScript prototype property allows you to add new properties to object constructors:

 对于Person来说,可以这样:Person.prototype.nationality = "English";   2.  Javascript Function 2.1  function的定义比较简单,掠过。参见:https://www.w3schools.com/js/js_function_definition.asp ,其中注意Arrow Functions,和Java中的语法基本一样。另外function也是Objects。 2.2 function的执行,比较常见,掠过。 Self-Invoking Functions的用法: (function () {
  let x = "Hello!!";  // I will invoke myself
})();
2.3 call(), apply() 方法:
     参见:https://www.w3schools.com/js/js_function_call.asp 2.4 bind()方法:      With the bind() method, an object can borrow a method from another object. 参见 https://www.w3schools.com/js/js_function_bind.asp ,但我的理解和这里描述的不同,我的理解是method把自己的instance bind到bind中的参数了,其中的例子中的member并没有新增fullName method.   TODO: 还有一个大点是JS中this的用法,这个很灵活,也很重要,参见:https://www.w3schools.com/js/js_this.asp ,有机会我再写下我的体会。    

 

标签:function,javascript,object,基础知识,Object,objects,new,一些,method
From: https://www.cnblogs.com/saaspeter/p/17129832.html

相关文章

  • HTML基础知识
    一HTML介绍1、基础相关:1、HTML:负责网页的架构2、CSS:负责网页的样式、美化3、JS:负责网页的行为2、什么是HTML1、HTML是用来......
  • 整理echarts的一些常用配置
    1.双Y轴当数据出现多条折线,而有的线条的数据很大,有的线条的数据很小,如果都统一放在一条Y轴上显示数据,则数据量小的会非常贴近X轴,这样就看不出数据的变化趋势,此时可......
  • [django]钩子函数的一些细节(clean)
    函数名说明:clean_后面跟着的是需要校验字段名称示例:classRelUserReset(forms.ModelForm):defclean_confirm_password(self):pass校验顺序说明:如果是继承的......
  • [javascript]端序(endian)和Buffer对象的read|write系列函数
    假设有如下对象:varbuf=Buffer.from("Hello.\n");其保存在内存当中的形式实际上是这样的,这里我们假设该对象的内存地址从0x00开始:地址0x000x010x020x030x04......
  • 2023前端开发最新面试题收集-Javascript篇
    前台、中台、后台-前台:面向用户、客户可以感知的,如商城-中台:可以看着对前台的补充,公共服务功能,如支付系统、搜索系统、客服-后台:面向运营、比如商品管理、物流管理1......
  • 盘点界面组件DevExtreme 2023年值得期待的一些新功能!
    DevExtreme拥有高性能的HTML5/JavaScript小部件集合,使您可以利用现代Web开发堆栈(包括React,Angular,ASP.NETCore,jQuery,Knockout等)构建交互式的Web应用程序,该套件附带功能......
  • 78JavaScript基础
    JavaScript操作DOM节点包括:JavaScript处理事件、操作节点、操作节点样式#demo.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><me......
  • javascript 高级编程系列 - async/await
    async/await其实是生成器的语法糖,async用于声明一个函数是异步的,而await用于等待一个异步方法执行完成,并且await只能出现在async函数中。1.async函数async函数返回一......
  • JavaScript 基础 - Day01
    了解变量、数据类型、运算符等基础概念,能够实现数据类型的转换,结合四则运算体会如何编程。体会现实世界中的事物与计算机的关系理解什么是数据并知道数据的分类理解......
  • 聊聊数学建模的这段经历及一些感想
    前言:很久没更新博客了。在这一篇文章中,我们主要聊一聊我和数学建模从相遇那天起到今日的这些事情。正文:1.相遇大一时在与学长学姐们的交流中了解到了一个叫做......