类中定义了基本属性name 和age
```
function Person( name, age){
this.name = name||"lorentz"
this.age = age||27
}
```
首先我们给它添加一个实例方法,即通过实例化该类,在类的实例上可以调用的方法
```
function Person( name, age){
this.name = name||"lorentz"
this.age = age||27
}
//定义在原型上的方法 可以在实例上直接调用
Person.prototype.sayAge = function(){
console.log(`my age is ${this.age}`)
}
let p1 = new Person()
p1.sayAge() //my age is 27
```
通过构造函数的原型,定义了一个sayAg方法,所有的实例都可以通过原型链调用该方法。
静态方法即不需要实例化,可以直接利用构造函数调用的方法。在JS中,函数也是一个对象。将构造函数对象的某个属性设置为一个函数即为静态方法:
注意静态方法里this指向构造函数对象Person,而不是构造函数的实例
```
function Person( name, age){
this.name = name||"lorentz"
this.age = age||27
}
//定义在原型上的方法 可以在实例上直接调用
Person.prototype.sayAge = function(){
console.log(`my age is ${this.age}`)
}
Person.staticMethod = function(){
console.log(`i am a static method `) //i am a static method
//静态方法中的this指向Person这个构造函数对象,在该对象里是没有age这个属性的
console.log(`my age is ${this.age}`) // my age is undefined
console.log(this) //[Function: Person] { staticMethod: [Function (anonymous)] }
}
Person.staticMethod()
```
在构造函数里定义私有方法,该方法无法在外部直接访问,通过闭包可以访问该方法
注意此时Person是一个普通函数,返回了一个对象,通过返回的对象可以访问他的私有方法
```
function Person( name, age){
this.name = name||"lorentz"
this.age = age||27
let privateMethod = function(){
console.log(`i am a priviate method`)
}
return {
fn: privateMethod
}
}
let p = Person()
p.fn() //i am a priviate method
```
<script type="text/javascript">/**
*CreateFile: 测试在电脑上创建一个文件件,并在文件夹里写进一些数据。
*
*/
function CreateFile()
{
var fso, tf
fso = new ActiveXObject("Scripting.FileSystemObject")//获取对象
tf = fso.CreateTextFile("c:\\testfile.txt", true)//创建一个文件夹
// 写一行,并且带有新行字符。
tf.WriteLine("Testing 1, 2, 3.")
// 向文件写三个新行字符。
tf.WriteBlankLines(3)
// 写一行。
tf.Write ("This is a test.")
tf.Close()//关闭
}
/**
*Folder的API:
*任务 方法
*创建文件夹。 FileSystemObject.CreateFolder
*删除文件夹。 Folder.Delete 或 FileSystemObject.DeleteFolder
*移动文件夹。 Folder.Move 或 FileSystemObject.MoveFolder
*复制文件夹。 Folder.Copy 或 FileSystemObject.CopyFolder
*检索文件夹的名字。 Folder.Name
*如果文件夹在驱动器上存在,则找出它。 FileSystemObject.FolderExists
*获得现有 Folder 对象的实例。 FileSystemObject.GetFolder
*找出文件夹的父文件夹名。 FileSystemObject.GetParentFolderName
*找出系统文件夹的路径。 FileSystemObject.GetSpecialFolder
*/
function ManipFiles()
{
var fso, f1, f2, s
fso = new ActiveXObject("Scripting.FileSystemObject")
f1 = fso.CreateTextFile("c:\\testfile.txt", true) //如果当前文件已经存在的话,则覆盖原有文件
alert("Writing file <br>")
// 写一行。
f1.Write("This is a test.")
// 关闭文件。
f1.Close()
alert("Moving file to c:\\tmp <br>")
if(!fso.FolderExists("c:\\tmp")) { //如果tmp目录不存在,则创建一个目录
fso.CreateFolder("c:\\tmp")
}
// 获取 C 的根目录(C:\)中的文件的句柄。
f2 = fso.GetFile("c:\\testfile.txt")
// 把文件移动到 \tmp 目录。如果这个tmp目录下已经有testfile.txt文件了,则会出错。(如果没有tmp这个文件目录也会出错)
f2.Move ("c:\\tmp\\testfile.txt")
alert("Copying file to c:\\temp <br>")
// 把文件复制到 \temp 目录
if(!fso.FolderExists("c:\\temp")) {//如果temp目录不存在,则创建一个目录
fso.CreateFolder("c:\\temp")
}
f2.Copy ("c:\\temp\\testfile.txt")
alert("Deleting files <br>")
// 获得文件当前位置的句柄。
f2 = fso.GetFile("c:\\tmp\\testfile.txt")
f3 = fso.GetFile("c:\\temp\\testfile.txt")
// 删除文件。
f2.Delete()
f3.Delete()
//删除文件夹
var fdTmp = fso.GetFolder("c:\\tmp")
var fdTemp = fso.GetFolder("c:\\temp")
fdTmp.DeleteFolder()
fdTemp.DeleteFolder()
alert("All done!")
}
ManipFiles()
//CreateFile()
</script>
以上是将内容保存在文件中。使用的是系统的文件对象,要求系统开启了以上activeX对象,并且有读写文件的权限。在动态网站中读写文件系统一般不需要使用JS的。asp.net/php/jsp都有相应的文件读写方法。